All technological notes.
Conditional Statement
| Situation to execute | Statement |
|---|---|
condition is true |
if |
condition is false |
else |
| new condition | else if |
| alternative conditiona | switch |
if StatementUse the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
else Statementelse statement to specify a block of code to be executed if the condition is false.if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
else if StatementUse the else if statement to specify a new condition if the first condition is false.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
switch Statementswitch statement to select one of many code blocks to be executed.The switch expression is evaluated once. The value of the expression is compared with the values of each case.
If multiple cases matches a case value, the first case is selected.
If no matching cases are found, the program continues to the default label.
If no default label is found, the program continues to the statement(s) after the switch.
Strict Comparison
Switch cases use strict comparison (===).
The values must be of the same type to match.
A strict comparison can only be true if the operands are of the same type.
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// code block
}
break KeywordWhen JavaScript reaches a break keyword, it breaks out of the switch block. This will stop the execution inside the switch block.
It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.
Note: If you omit the break statement, the next case will be executed even if the evaluation does not match the case.
default KeywordThe default keyword specifies the code to run if there is no case match.
The default case does not have to be the last case in a switch block.
default is not the last case in the switch block, remember to end the default case with a break.console.log("\n-------- switch --------\n");
switch (new Date().getDay()) {
default:
console.log("Weekday");
break;
case 6:
console.log("Saturday");
break;
case 0:
console.log("Sunday");
}
console.log("\n-------- switch --------\n");
switch (new Date().getDay()) {
case 4:
case 5:
console.log("Soon it is Weekend");
break;
case 0:
case 6:
console.log("It is Weekend");
break;
default:
console.log("Looking forward to the Weekend");
}