Skip to content

Switch Statements

A switch statement compares one value against a list of case values and runs the code for the first one that matches. It is an alternative to a long else if chain that keeps testing the same value. A default clause, which is optional, runs when no case matches.

const describeOrder = (status) => {
switch (status) {
case 'placed':
return 'We have your order.';
case 'packed':
return 'Your order is ready to send.';
case 'shipped':
return 'Your order is on its way.';
default:
return 'Unknown status.';
}
};
console.log(describeOrder('packed'));
console.log(describeOrder('lost'));

This prints:

Your order is ready to send.
Unknown status.

Once a case matches, JavaScript runs every statement after it, through the following cases too, until it reaches a break, a return or the end of the switch. This is fall-through. The example above needs no break because each case returns, but without either, the cases run on:

const size = 'M';
switch (size) {
case 'S':
console.log('small');
case 'M':
console.log('medium');
case 'L':
console.log('large');
default:
console.log('no size chosen');
}

This prints:

medium
large
no size chosen

A missing break is the most common switch bug, so end every case with break or return.

Fall-through is useful when several values share one outcome. Cases with no code of their own fall through to the next case that has some:

const day = 'Tuesday';
switch (day) {
case 'Saturday':
case 'Sunday':
console.log('Weekend opening hours: 10:00 to 16:00');
break;
case 'Monday':
case 'Tuesday':
case 'Wednesday':
case 'Thursday':
case 'Friday':
console.log('Weekday opening hours: 08:00 to 20:00');
break;
default:
console.log('Not a day of the week');
}

This prints:

Weekday opening hours: 08:00 to 20:00

switch compares with strict equality, so a string never matches a number:

const quantity = '2';
switch (quantity) {
case 2:
console.log('two, as a number');
break;
default:
console.log('no match');
}

This prints:

no match

Convert the value first, as in switch (Number(quantity)), when it may arrive as a string.

All the cases share one block, so declaring the same const or let name in two cases is a SyntaxError. Wrap each case’s code in braces to give it a block of its own:

const shape = { type: 'circle', radius: 2 };
switch (shape.type) {
case 'circle': {
const area = Math.PI * shape.radius ** 2;
console.log(area.toFixed(2));
break;
}
case 'square': {
const area = shape.side ** 2;
console.log(area);
break;
}
}

This prints:

12.57

A switch cannot test a range directly. One workaround is switch (true) with a condition in each case, so the first condition that gives true matches:

const temperature = 18;
switch (true) {
case temperature < 0:
console.log('freezing');
break;
case temperature < 15:
console.log('cold');
break;
default:
console.log('mild or warm');
}

This prints:

mild or warm

It works, but an else if chain says the same thing in a more familiar form. Keep switch for matching one value against fixed values.

Steve Griffith covers this topic in his video JavaScript Switch Case Statements.