In JavaScript, conditions are used to control the flow of your code based on certain criteria. The most common type of condition is the if statement, but JavaScript also supports other types of conditions like the switch statement, the ternary operator, and the logical operators.
Here's a detailed guide to JavaScript conditions:
- The if statement: The if statement is used to execute a block of code if a specified condition is true. Here's the syntax for the if statement:
scssif (condition) {
// code to be executed if the condition is true
}
Here's an example that uses the if statement to check if a number is even:
javascriptlet number = 10;
if (number % 2 === 0) {
console.log('The number is even.');
}
In this example, the code inside the if statement will be executed because the condition (number % 2 === 0)
is true.
- The else statement: The else statement is used in conjunction with the if statement to execute a block of code if the condition is false. Here's the syntax for the if...else statement:
vbnetif (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Here's an example that uses the if...else statement to check if a number is even or odd:
javascriptlet number = 5;
if (number % 2 === 0) {
console.log('The number is even.');
} else {
console.log('The number is odd.');
}
In this example, the code inside the else statement will be executed because the condition (number % 2 === 0)
is false.
- The else if statement: The else if statement is used to specify a new condition to test if the previous condition is false. Here's the syntax for the if...else if statement:
vbnetif (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}
Here's an example that uses the if...else if statement to check if a number is positive, negative, or zero:
javascriptlet number = -5;
if (number > 0) {
console.log('The number is positive.');
} else if (number < 0) {
console.log('The number is negative.');
} else {
console.log('The number is zero.');
}
In this example, the code inside the else if statement will be executed because the condition (number < 0)
is true.
- The switch statement: The switch statement is used to execute a block of code depending on the value of an expression. Here's the syntax for the switch statement:
arduinoswitch (expression) {
case value1:
// code to be executed if expression === value1
break;
case value2:
// code to be executed if expression === value2
break;
default:
// code to be executed if expression is different from all cases
}
Here's an example that uses the switch statement to check the day of the week:
javascriptlet day = 'Monday';
switch (day) {
case 'Monday':
console.log('Today is Monday.');
break;
case 'Tuesday':
console.log('Today is Tuesday.');
break;
case 'Wednesday':
console.log('Today is Wednesday.');
break;
case 'Thursday':
console.log('Today is Thursday.');
break;