If Statementโ
Programming conditions are used to perform different actions based on different situations. For example, you can use an if statement to execute some code only if a certain condition is true ๐, or an else statement to execute some code if the condition is false ๐. You can also use an else if statement to test multiple conditions and choose the appropriate action for each one ๐ค.
An else if statement is like a detour in the road ๐. You can use it to check more than one sign ๐ฆ and choose different paths based on them. For example, if you see a pizza place ๐, you might want to go there; else if you see a burger place ๐, you might want to go there; else, you might want to go somewhere else ๐ฎ.
The syntax (js)โ
if (sign1) {
// code to do if sign1 is true โ
} else if (sign2) {
// code to do if sign2 is true โ
} else {
// code to do if both signs are false โ
}
- You can use as many else if statements as you want, but you can only have one else statement at the end. The code will check the signs from top to bottom and stop at the first one that is true โ . If none of them are true โ, it will do the code in the else statement.
- Here is an example of an else if statement in JavaScript:
var weather = "rainy";
if (weather == "sunny") {
console.log("You are wearing sunglasses ๐");
} else if (weather == "cloudy") {
console.log("You are wearing a hat ๐งข");
} else if (weather == "rainy") {
console.log("You are wearing a raincoat ๐งฅ");
} else {
console.log("You are wearing whatever you want ๐");
}
I hope this helps you understand the else if statement. ๐
Check many "or (||)" statements at onceโ
Combine multiple conditionsโ
One way is to use the logical OR operator (||) to combine multiple conditions in an if statement, like this:
if (condition1 || condition2 || condition3) {
// code to execute if any of the conditions is true โ
} else {
// code to execute if none of the conditions is true โ
}
For example:
var age = 18;
if (age == 18 || age == 21 || age == 25) {
// code to execute if age is 18, 21 or 25 โ
} else {
// code to execute if age is not 18, 21 or 25 โ
}
var color = "red";
if (color == "red" || color == "green" || color == "blue") {
// code to execute if color is red, green or blue โ
} else {
// code to execute if color is not red, green or blue โ
}
.some()โ
Another way is to use the array method .some() to check if any of the elements in an array satisfies a function, like this:
var conditions = [condition1, condition2, condition3];
if (
conditions.some(function (condition) {
return condition; // return true or false
})
) {
// code to execute if any of the conditions is true โ
} else {
// code to execute if none of the conditions is true โ
}
For example:
var str = "hello";
var conditions = ["hello", "hi", "howdy"];
if (
conditions.some(function (condition) {
return str.includes(condition); // return true or false
})
) {
// code to execute if str contains hello, hi or howdy โ
} else {
// code to execute if str does not contain hello, hi or howdy โ
}
var num = 10;
var conditions = [5, 10, 15];
if (
conditions.some(function (condition) {
return num % condition == 0; // return true or false
})
) {
// code to execute if num is divisible by 5, 10 or 15 โ
} else {
// code to execute if num is not divisible by 5, 10 or 15 โ
}
Switch statementโ
A third way is to use a switch statement with multiple cases for each condition, like this:
switch (true) {
case condition1:
case condition2:
case condition3:
// code to execute if any of the conditions is true โ
break;
default:
// code to execute if none of the conditions is true โ
}
For example:
var animal = "dog";
switch (true) {
case animal == "dog":
case animal == "cat":
case animal == "rabbit":
// code to execute if animal is dog, cat or rabbit โ
break;
default:
// code to execute if animal is not dog, cat or rabbit โ
}
var score = 80;
switch (true) {
case score >= 90:
case score >= 80:
case score >= 70:
// code to execute if score is greater than or equal to 90, 80 or 70 โ
break;
default:
// code to execute if score is less than 70 โ
}
I hope this helps you understand how to check many or statements in JavaScript. If you have any questions or need more examples, please let me know. ๐