If leftExpr is null or undefined, a is equal to rightExpr
Otherwise a is equal to leftExpr
let a =null;// long versionif(a ===null|| a ===undefined){
a =10;}
console.log(`a = ${a}`);// nullish coalescing operator
a = a ??10;
console.log(`a = ${a}`);// or even shorter with the 'logical nullish assignment'
a ??=10;
console.log(`a = ${a}`);
condition ? 'valueTrue' : 'valueFalse' provides a shorthand for an if else statement
If the condition evaluates to true, the result of the operator is valueTrue
Otherwise (condition evaluates to false), the result of the operator is valueFalse
Example 1:
const a =10;const b =7;// long versionif(a >5){
console.log(`${a} > ${b}`);}else{
console.log(`${a} <= ${b}`);}// shorthand version with ternary operator
a >5? console.log(`${a} > ${b}`): console.log(`${a} <= ${b}`);
1 2 3 4 5 6 7 8 9 10 11 12
Example 2:
const a =4;let isEven =null;// long versionif(a %2===0){
isEven ='even';}else{
isEven ='odd';}
console.log(`${a} an ${isEven} number`);// shorthand version with ternary operator
isEven = a %2===0?'even':'odd';
console.log(`${a} an ${isEven} number`);
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Example 3:
const a =4;let isEven =null;// long versionif(a %2===0){
isEven =true;}else{
isEven =false;}
console.log(`${a} is even? ${isEven}`);// shorthand version
isEven = a %2===0?true:false;
console.log(`${a} is even? ${isEven}`);// if the return value is 'true' of 'false' you can shorten this even more
isEven = a %2===0;
console.log(`${a} is even? ${isEven}`);
if(condition1){// block of code to be executed if condition1 is true}elseif(condition2){// block of code to be executed if condition1 is false and condition2 is true}elseif(condition3){// block of code to be executed if condition1 and condition2 are false and condition3 is true}else{// block of code to be executed if none of the previous conditions are true}
switch(expression){case x:// block of code to be executed if case x is truebreak;case y:// block of code to be executed if case y is truebreak;default:// block of code to be executed if none of the previous cases are true}
There are several ways to loop over an array, but the most interesting (and the only one we use in this course) is the forEach(opens new window) iteration
The callback function can have three parameters:
element: the current element being processed in the array
index (optional): the index of the current element
array (optional): the full array on which forEach is performed
const array =['element1','element2','element3',...];
array.forEach(function(element, index, array){// block of code to be executed on the current element})
Line 4: listen to the blur event on the input field
(Note that a blur event corresponds with a click outside the input field, in contrast to a focus event where you click inside the input field)
Line 6: set score to null if the input field is empty
Line 11 to 23: the score value determines which message is displayed in which colour
Line 25: update the message
Line 26: remove the previous class on #result
Line 27: add the new class on #result
Line 31: trigger the blur event on page load
const scoreInput = document.getElementById('score');const result = document.getElementById('result');
scoreInput.addEventListener('blur',function(e){// ternary operatorconst score = scoreInput.value ===''?null: scoreInput.value;let scoreMsg =null;let scoreClass ='border-gray';// if - else ifif(score ===null){
scoreMsg ='-- NO SCORE AVAILABLE --';}elseif(score >=10){
scoreMsg =`<b>${score}/20</b>: you passed the JavaScript course. Congratulations!`;
scoreClass ='border-green';}elseif(score >=8){
scoreMsg =`<b>${score}/20</b>: you failed the JavaScript course, but you may tolerate it!`;
scoreClass ='border-blue';}else{
scoreMsg =`<b>${score}/20</b>: unfortunately, you failed the JavaScript course. Better luck next time!`;
scoreClass ='border-red';}// show result
result.innerHTML =`<p>${scoreMsg}</p>`;
result.className ='';
result.classList.add(scoreClass);});// dispatch (trigger) blur event
scoreInput.dispatchEvent(newEvent('blur'));