JS Conditional Statements
Download (.odt) Download (Markdown)Return a boolean (true or false) value.
Bigger/smaller
[hook]Smaller than:
let variable = 69;
let variable2 = 21;
console.log(variable < 69); // false
console.log(variable < variable2); // false
Bigger than:
// variable = 69 and variable2 = 21
console.log(variable > 69); // false
console.log(variable > variable2); // true
Smaller than or equal to:
// variable = 69 and variable2 = 21
console.log(variable <= 69); // true
console.log(variable <= variable2); // false
Bigger than or equal to:
// variable = 69 and variable2 = 21
console.log(variable >= 69); // true
console.log(variable >= variable2); // true
Equality
[hook]Equality with data type conversion:
// variable = 69
console.log(variable == "69"); // returns true because the string "69" is converted to the number 69
console.log(variable == 69); // returns true
Equality without data type conversion (strict):
// variable = 69
console.log(variable === "69"); // returns false because there are two different types of data provided
console.log(variable === 69); // returns true
Negative equality with data type conversion:
// variable = 69
console.log(variable != "69"); // returns false because the string "69" is converted to the number 69
console.log(variable != 69); // returns false
Negative equality without data type conversion (strict):
// variable = 69
console.log(variable !== "69"); // returns true because there are two different types of data provided
console.log(variable !== 69); // returns false
Logical operators
[hook]Logical AND (both conditions have to return true):
// variable = 69 and variable2 = 21
console.log(variable > 18 && variable2 > 18); // returns true because both conditions return true
console.log(variable === 69 && variable2 === 69); // returns false because the second condition returns false
Logical OR (at least one condition has to return true):
// variable = 69 and variable2 = 21
console.log(variable > 18 || variable2 > 18); // returns true because both conditions return true
console.log(variable === 69 || variable2 === 69); // returns true because the first condition returns true
Usecases
[hook]If-else statement
// variable = 69
if (variable > 69) {
console.log("It's bigger than 69");
} else if (variable < 69) {
console.log("It's smaller than 69");
} else {
console.log("It's equal to 69");
}
// Evaluates the given conditions in order and runs the code block belonging to the first true condition then quits. Else if and else blocks are optional, there can be unlimited number of else if blocks. In this case none of the conditions return true so it runs the else block and prints the string "It's equal to 69" to the console.
Switch statement
variable = 21
switch (variable) {
case 18: console.log("Adult");
break;
case 21: console.log("Fingers");
break;
case 69: console.log("You pervert!");
break;
default: console.log("Boring number");
}
// Compares variable to each cases using strict equality (variable === 18, variable === 21 etc.) and runs the following piece of code. In this case the second case is true so it prints the string "Fingers" to the console.
If a break is missing after a case:
// variable = 21
switch (variable) {
case 18: console.log("Adult");
break;
case 21: console.log("Fingers");
case 69: console.log("You pervert!");
break;
default: console.log("Boring number");
}
// The second case is true so it prints the string "Fingers" to the console. Then continues with the third case because there's no break between them so it also prints the string "You Pervert!".
One piece of code is missing from a case:
// variable = 21
switch (variable) {
case 18: console.log("Adult");
break;
case 21:
case 69: console.log("You pervert!");
break;
default: console.log("Boring number");
}
// The second case is true but there's no code to run so it continues to the next case and prints the string "You pervert!" to the console. (If there would be a break between the two cases it would print nothing to the console.)
Ternary operator
Assign a value based on a conditional statement to a variable.
let age = 30;
let accessAllowed = (age >= 18) ? true : false; // true
Nested ternary operator:
let number = 21;
let positiveNegative = (number > 0) ? "positive" : (number < 0) ? "negative" : 0; // "positive"