KandZ – Tuts

We like to help…!

JavaScript 12 🧬 switch statement

a - switch statement info
switch statement is used to execute different blocks of code based on the value of an expression.
It provides a more concise and readable way to handle multiple conditions compared to using multiple if-else statements.
syntax

switch (expression) {
case value1:
// Code to be executed if expression === value1
break;
case value2:
// Code to be executed if expression === value2
break;
case value3:
// Code to be executed if expression === value3
break;
default:
// Code to be executed if none of the cases match
}

Expression → The switch statement evaluates this expression.
Case Labels → Each case label specifies a possible value for the expression.
Break Statement → The break statement is used to terminate the switch block and prevent fall-through.
If you omit the break, the execution will continue into the next case, even if it doesn't match.
Default Case → The default label specifies a default set of code to be executed if none of the case labels match the expression.

b - switch examples
Normal example
Output: It's an apple!
Fall-through Example:
Output: Tuesday
Wednesday
Weekend
Grouping Example
Output: Large number

Leave a Reply