Open In App
Related Articles

JavaScript switch Statement

Improve Article
Improve
Save Article
Save
Like Article
Like

JavaScript switch statement is used to execute a block of code from multiple expressions.

JavaScript switch statement evaluates an expression. The expression’s value is compared with the values of each case in the structure. If a match is found, the related block of code is executed.

Usage of JavaScript switch statement

JavaScript switch statement is used with a break or default keyword (optional and both can be used together also).

  • break: This keyword is used to break out of the switch block. This stops the execution inside the code block.
  • default: This keyword is used to specify a piece of code if no case matches the given condition. There can be only one default keyword in a switch statement.

Syntax

switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}

Parameters

  • expression: The expression which is to be evaluated.

Explanation

  • The expression can be of type numbers or strings.
  • Duplicate case values are not allowed.
  • The default statement is optional. If the expression passed to the switch does not match the 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.
  • Cases are compared strictly.

Flowchart

Example 1: In this example, we will check our I value with the help of a switch case.

JavaScript




// JavaScript program to illustrate switch-case
let i = 9;
 
switch (i) {
    case 0:
        console.log("i is zero.");
        break;
    case 1:
        console.log("i is one.");
        break;
    case 2:
        console.log("i is two.");
        break;
    default:
        console.log("i is greater than 2.");
}


Output

i is greater than 2.

Example 2: In this example, we will check our grade by using a switch case.

Javascript




let grade = 'B';
let result;
switch (grade) {
    case 'A':
        result = "A (Excellent)";
        break;
    case 'B':
        result = "B (Average)";
        break;
    case 'C':
        result = "C (Below than average)";
        break;
    default:
        result = "No Grade";
}
console.log(result);


Output

B (Average)

Common Code Blocks

In some cases, we need to use the same code for multiple switch cases. Let’s see an example of how to do it:

Example: In this example, we will same code blocks for two different switch cases.

Javascript




let grade = 'A'
let result;
 
switch (grade) {
    case 'A':
        result = "Grade is excellent"
        break;
    case 'B': ;
    case 'C': ;
        result = "Grade is Average "
        break;
    case 'D':
        result = "Grade is Poor"
        break;
    default:
        text = "NO grades achieved";
}
console.log(result)


Output

Grade is excellent

Note: If there are multiple switch cases that match a value, the first is executed.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 Nov, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials