The switch Statement part 1

JavaScript Tutorial
Conditionals and Loops
The switch Statement
30

1/4
               

Switch



In cases when you need to test for multiple conditions, writing if else statements for each condition might not be the best solution.
The switch statement is used to perform different actions based on different conditions.

Syntax:switch (expression) {
case n1:
statements
break;
case n2:
statements
break;
default:
statements
}

The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.
You can achieve the same result with multiple if...else statements, but the switch statement is more effective in such situations.
The switch statement can be used to replace...
comments
multiple if else statements
variable declarations

Comments

Popular posts from this blog

Comparison Operators part 2

Break and Continue part 1

Module 2