Switch Case in JavaScript
We have learned about decision making in JavaScript using if-else statements in our previous article on if-else statement in JavaScript. We have seen in our previous article that we can use the if-else statements to perform actions based on some particular condition. That is if a condition is true then perform some task or else if the condition is false then execute some other task.
The switch case statement in JavaScript is also used for decision making purposes. In some cases, using the switch case statement is seen to be more convenient over if-else statements. Consider a situation when we want to test a variable for hundred different values and based on the test we want to execute some task. Using if-else statement for this purpose will be less efficient over switch case statements and also it will make the code look messy.
The switch case statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
Explanation:
- expression can be of type numbers or strings.
- Dulplicate case values are not allowed.
- The default statement is optional. If the expression passed to switch does not matches with value in any case then the statement under default will be executed.
- The break statement is used inside the switch to terminate a statement sequence.
- The break statement is optional. If omitted, execution will continue on into the next case.
Flowchart:

Example:
<script type = "text/javascript"> // JavaScript program to illustrate switch-case var i = 9; switch (i) { case 0: document.write("i is zero."); break; case 1: document.write("i is one."); break; case 2: document.write("i is two."); break; default: document.write("i is greater than 2."); } </script></div> |
Output:
i is greater than 2.
Recommended Posts:
- Case insensitive search in JavaScript
- How to convert hyphens to camel case in JavaScript ?
- Convert string to title case in JavaScript
- How to convert string to camel case in JavaScript ?
- Compare the Case Insensitive strings in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


