Conditional statements¶
Conditional statements are language built-in mechanisms that execute the selected code depending on whether the value of the expression is true (true
) or false (false
). JavaScript offers the following options:
if
if
-else
else if
switch
if¶
These instructions have a similar syntax to what you already know from Java. Ie. after the keyword if
in parentheses we give the appropriate condition. If it evaluates to true
then the code from the inner block (i.e. inside the {
and }
braces) will be executed, otherwise it is skipped, e.g .:
if (1 === 1) {
console.log('Tak 1 jest rowne 1!'); // this line will be displayed on the screen
}
if ([1, 2].includes(1)) {
console.log('Yes, this array contains the number 1'); // this line will be displayed on the screen
}
if ('') {
console.log('an empty string evaluates to false'); // this line will NOT be displayed on the screen
}
if ({} && []) {
console.log('An empty array and an empty object evaluate to true'); // this line will NOT be displayed on the screen
}
if (null || undefined) {
console.log('null and undefined evaluate to false'); // this line will NOT be displayed on the screen
}
if else and else¶
Each if
conditional statement can define multiple blocks of code that will be executed when any of the preceding conditions following the word if
evaluates to false
. These blocks are else if
. It is also possible to define a block of code after the keyword else
, which will be executed in case none of the previous conditions in if
orelse if
blocks will be met, e.g.:
const number = 5;
if (number % 5 === 4) {
console.log('is your number 9?');
} else if (number % 5 === 3) {
console.log('The remaiander is 3!');
} else if (number > 5) {
console.log('I dont think its possible');
} else {
console.log('All \'ifs \ were false so we landed here');
}
Switch statement¶
The switch
statement in JavaScript performs the same function and has a very similar syntax as in [Java] (../../java_basics/control_statements.md # switch-statement).It plays the role of selecting the appropriate block to be executed based on the specified value. Each possibility is printed using the case
keyword, followed by the code that should be executed after a colon. We can end a block of this code with a break
statement, which will exit theswitch
statement. This statement also offers the option of defining a default
block, which will be executed in case the value does not match any of the definedcase
blocks.
For example:
const value = 5;
switch (value) {
case 2:
case 4:
console.log('I am definitely even and lower than 5');
break;
case 1:
case 3:
console.log('I am odd and lower than 5');
break;
case 5:
console.log('I am equal to 5');
break;
default:
console.log('I am greater than 5');
}
// will be displayed: I am equal to 5
NOTE: The
if else
statements, together withelse
, work just like theswitch
statement, and we can use them interchangeably. If we useif else
statements when the number ofif else
blocks increases significantly, consider whether theswitch
statement will be clearer.