switch
switch
Section titled “switch”Introduction
Section titled “Introduction”The switch statement evaluates an expression and executes code based on matching cases. It’s cleaner than multiple if-else chains when comparing the same value.
Syntax
Section titled “Syntax”switch (expression) { case value1: // runs if expression === value1 break; case value2: // runs if expression === value2 break; default: // runs if no case matches}Visual Explanation
Section titled “Visual Explanation”flowchart TD Exp["Day of week"] Mon["Monday → Work"] Tue["Tuesday → Work"] Sat["Saturday → Party"] Sun["Sunday → Rest"] Default["Invalid day"]
Exp --> Mon Exp --> Tue Exp --> Sat Exp --> Sun Exp --> DefaultExample
Section titled “Example”const day = 3;
switch (day) { case 1: console.log('Monday'); break; case 2: console.log('Tuesday'); break; case 3: console.log('Wednesday'); break; default: console.log('Invalid day');}Fall-Through
Section titled “Fall-Through”switch (day) { case 1: case 2: case 3: case 4: case 5: console.log('Weekday'); break; case 6: case 7: console.log('Weekend'); break;}Summary
Section titled “Summary”- Use
switchfor multiple conditions on the same value - Always include
breakunless you want fall-through defaulthandles no-match cases