In this tutorial you will learn how to use the switch...case statement to test or evaluate an expression with different values in JavaScript.
In this tutorial you will learn how to write the decision-making code using if...else...else if conditional statements in JavaScript.
JavaScript Conditional Statements
Like many other programming languages, JavaScript also allows you to write code that perform different actions based on the results of a logical or comparative test conditions at run time. This means, you can create test conditions in the form of expressions that evaluates to either true or false and based on these results you can perform certain actions.
Using the Switch...Case Statement
The switch..case statement is an alternative to the if...else if...else statement, which does almost the same thing. The switch...case statement tests a variable or expression against a series of values until it finds a match, and then executes the block of code corresponding to that match. It's syntax is:
switch(x){
case value1:
// Code to be executed if x === value1
break;
case value2:
// Code to be executed if x === value2
break;
...
default:
// Code to be executed if x is different from all values
}