Switch Case

The switch statement evaluates an expression and matches it against multiple cases.

Basic Syntax

const day = 'Monday'

switch (day) {
  case 'Monday':
    console.log('Start of the work week')
    break
  case 'Friday':
    console.log('Last day before weekend')
    break
  case 'Saturday':
  case 'Sunday':
    console.log('Weekend!')
    break
  default:
    console.log('Midweek')
}

Important: break

Without break, execution falls through to the next case:

switch (1) {
  case 1:
    console.log('one')
    // no break!
  case 2:
    console.log('two')
    break
  case 3:
    console.log('three')
}
// Output: 'one', 'two'  (falls through!)

Switch vs if/else

Switch uses strict equality (===) for comparison:

switch ('1') {
  case 1:
    console.log('number')
    break
  case '1':
    console.log('string')  // ← this matches
    break
}