zhaoJian's Tech Notes

IT Course JavaScript Basics 041_Conditional Statements if, else, switch

Learning / JavaScript Basics ~3520 words · 9 min read - views

Conditional statements in JavaScript mainly include if, else if, else, and switch.

if Statement

The if statement is used to determine whether to execute a piece of code based on a condition. When the conditional expression in the if(...) statement parentheses is true, one line of statement after if is executed. If you want to control multiple lines of code, you can use {} to enclose the statements. It is recommended to always use curly braces {} to wrap code blocks when using if statements, even if there is only one statement, as this improves code readability. If the conditional expression is false, it will not execute. If the conditional expression after if is not a boolean value, it will first be converted to a boolean value before evaluation. The number 0, empty string "", null, undefined, and NaN are all converted to false. Other values are converted to true.

Example:

let age = 18;
if (age >= 18) console.log('Adult');
if (age >= 18) {
console.log('Adult');
}

if-else Statement

The if-else statement can execute different code blocks based on different conditions. If the expression evaluates to true, the code block after if is executed; otherwise, the code block after else is executed.

Example:

let age = 25;
if (age >= 18) {
console.log('Adult');
} else {
console.log('Minor');
}

else if Statement

The else if statement is an extension used in the if-else structure, allowing you to add multiple conditions for evaluation. The else if statement evaluates the conditional expressions after if from top to bottom until a true condition appears. When a true condition appears, subsequent conditions will not continue to be evaluated, so you need to properly design the order of conditions or add logical operation conditions.

Example:

let score = 95;
if (score >= 90) {
console.log('Excellent');
} else if (score >= 60) { //Try moving this condition to first and see what happens?
console.log('Pass');
} else {
console.log('Fail');
}

switch Statement

The switch statement can replace multiple if statements for multi-branch selection based on the value of an expression. When the switch statement executes, it compares the expression after switch with the expressions after case using strict equality. If the comparison result is true, the switch statement executes the code block after the corresponding case until it encounters the nearest break statement (or until the end of the switch statement). If there is no matching case, the default code block is executed (if default exists).

Example:

let day = 7;
switch (day) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
break;
case 6: //case grouping
case 7: //Several case branches sharing the same code can be grouped together
console.log('Weekend');
break;
default:
console.log('Other');
}

break Terminates (Ends, Exits) Loop

break is used to terminate (end, exit) the current loop or switch statement. If there is no break, subsequent case statements will continue to execute (i.e., “fall-through” occurs).

Example:

let day = 2;
switch (day) {
case 1:
console.log('Monday');
break;
case 2:
console.log('Tuesday');
// No break, will continue to execute the next case
case 3:
console.log('Wednesday');
break;
default:
console.log('Other');
}
Share:

Comments