A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/switch-case-in-javascript/ below:

JavaScript switch Statement - GeeksforGeeks

JavaScript switch Statement

Last Updated : 02 Jul, 2025

The JavaScript switch statement evaluates an expression and executes a block of code based on matching cases. It provides an alternative to long if-else chains, improving readability and maintainability, especially when handling multiple conditional branches.

Switch Statement Example: Here, we will print the day name on day 3.

JavaScript
let day = 3;
let dayName;

switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    case 4:
        dayName = "Thursday";
        break;
    case 5:
        dayName = "Friday";
        break;
    case 6:
        dayName = "Saturday";
        break;
    case 7:
        dayName = "Sunday";
        break;
    default:
        dayName = "Invalid day";
}

console.log(dayName); // Output: Wednesday
Explanation: Switch Statement Syntax
switch (expression) {
case value1:
// code block 1;
break;
case value2:
// code block 2;
break;
...
default:
// default code block;
}
How Switch Statement Works Flowchart of Switch Statement

Here, 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);
Explanation: Break Keyword

The break the keyword is used to terminate the execution of a loop or a switch statement.

default Keyword

The default the keyword is used within a switch statement as a fallback option when none of the case expressions match the value being evaluated. It acts similarly to the else statement in an if...else chain, providing a default action to take when no other specific cases match.

Position of default case does not matter:

Regardless of its placement, the default case only gets executed if none of the other case conditions are met. So, putting it at the beginning, middle, or end doesn't change the core logic (unless you're using a less common technique called fall-through).

we will print the default case.

JavaScript
let day = 8;
let dayName;

switch (day) {
    default:
        dayName = "Invalid day";
        break;
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    case 3:
        dayName = "Wednesday";
        break;
    case 4:
        dayName = "Thursday";
        break;
    case 5:
        dayName = "Friday";
        break;
    case 6:
        dayName = "Saturday";
        break;
    case 7:
        dayName = "Sunday";
        break;
    
}

console.log(dayName);
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:

Common Code Blocks Example:

Here, we will same code blocks for two different switch cases.

JavaScript
let grade = 'B'; 
let result;

switch (grade) {
    case 'A':
    case 'B':
    case 'C':
        result = "Grade is good"; 
        break;
    case 'D':
        result = "Grade is Poor";
        break;
    default:
        result = "No grades achieved";  
}

console.log(result);  

Output

Grade is good
Explanation:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4