Skip to main content

Js classroom - conditions ๐Ÿšฆ

ยท 3 min read
Swkeep

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 ๐Ÿค”.

info

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. ๐Ÿ˜Š