JavaScript switch Statement
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.
Table of Content
Switch Statement Syntax
switch (expression) {
case value1:
// code block 1;
break;
case value2:
// code block 2;
break;
...
default:
// default code block;
}
Expressionis the value that you want to compare.Case value1,case value2, etc., represent the possible values of theexpression.breakstatement terminates theswitchstatement. Without it, execution will continue into the next case.Defaultspecifies the code to run if none of the cases match theexpression.
How Switch Statement Works
- Evaluation: The expression inside the
switchthe statement is evaluated once. - Comparison: The value of the expression is compared with each
caselabel (using strict equality===). - Execution: If a match is found, the corresponding code block following the matching
casethe label is executed. If no match is found, the execution jumps to thedefaultcase (if present) or continues with the next statement after theswitchblock. - Break Statement: After executing a code block, the
breakstatement terminates theswitchstatement, preventing execution from falling through to subsequent cases. Ifbreakis omitted, execution will continue to the next case (known as “fall-through”). - Default Case: The
defaultcase is optional. If no match is found, the code block underdefaultis executed.
Flowchart of Switch Statement

Switch Statement Example:
Here, we will print the day name on day 3.
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
Output
Wednesday
Explanation:
Dayis set to3.- The
switchstatement evaluatesday. - Since
dayis3, thecase 3the block is executed, assigning"Wednesday"todayName. - The
breakstatement ends theswitchstatement, preventing execution from continuing into other cases.
Switch Statement Example:
Here, we will check our grade by using a switch case.
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)
Explanation:
Gradeis assigned the value'B'.- The
switchstatement evaluates the value ofgrade. - Since
gradeis'B', the code block followingcase 'B':is executed. - The
resultthe variable is assigned the string"B (Average)". - The
breakstatement terminates theswitchstatement. resultis logged to the console, which outputs"B (Average)".
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.
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);
Output
Invalid day
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.
let grade = 'A'
let result;
switch (grade) {
case 'A':
result = "Grade is excellent"
break;
case 'B':
result = "Grade is good"
break;
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
Explanation:
Gradeis assigned the value'A'.- The
switchstatement evaluates the value ofgrade. - Since
gradematches'A', the code block followingcase 'A':is executed, settingresultto"Grade is excellent". - The
breakstatement terminates theswitchstatement. Resultis logged to the console, which outputs"Grade is excellent".
Note: If multiple switch cases match a value, the first is executed.
JavaScript switch Statement-FAQs
Why do we use the break statement in a switch block?
The break statement is used to terminate the switch block once a matching case is executed. Without break, the code will continue executing the subsequent cases (known as “fall-through”), even if they don’t match.
What happens if there is no matching case in a switch statement?
If no cases match, the default block (if provided) will be executed. If there’s no default block, no code runs if there are no matches.
Can I use a switch statement with non-integer values like strings or objects?
Yes, the switch statement can handle strings, booleans, and other data types, but it compares using strict equality (===), meaning the types must match exactly.
How is the switch statement different from if-else statements?
The switch statement is often more readable and organized when dealing with multiple conditions based on a single expression, while if-else is better for more complex or range-based conditions.
Can a switch statement handle range conditions (e.g., x > 10)?
No, the switch statement is designed for exact matches. For range conditions, use if-else statements or include logical expressions within the cases.

