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
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!