Switch Statement
The switch statement lets us run a statement based on the return value of an expression. It’s the same as having a series of if statements.
In this article, we’ll look at how to use switch statements.
Defining Switch Statements
switch statements are composed of a switch block with case statements for each case.
For example, we can define it as follows:
const person = 'Joe';
switch (person) {
case 'Jane':
console.log('Hi Jane');
break;
case 'John':
console.log('Hi John');
break;
case 'Joe':
console.log('Hi Joe');
break;
case 'Mary':
console.log('Hi Mary');
break;
default:
console.log('Hi');
break;
}A switch block consists of the switch keyword, which tells us that we’re creating a switch block.
After that, we have the expression that we want to compare in parentheses. In our example, we want to compare person against the values in the case expressions below.
Then we have the case clauses, which we compare the values after to person.
case 'Jane' compares 'Jane' to person for equality, case 'John' compares 'John' to person for equality, and so on.
In each case clause, we have a break; line so that the rest of the code doesn’t get executed once a match is found.
The code above should log 'Hi Joe' because we set person to 'Joe'.
The default clause executes person doesn’t match any of the values listed in the case expressions above. This means it’ll run if person isn’t 'Jane;, 'John', 'Joe', or 'Mary'.
Therefore, the switch statement above is the same as:
The case and default clauses are optional, but if we don’t define any of them, then the switch statement wouldn’t be very useful.
Putting Default Between Cases
We can put the default clause between cases and the switch statement still works the same way.
So:
gets the same result as we have in the original example.
Multi-Criteria Case
We can have case clauses that are compared to multiple values for equality.
For example, if we have:
Then we get 'I like this number' since foo is 1, which is one of the values in the first case clause.
If we set foo to -1, then we get 'I love this number'.
Chained Expressions
A useful use case for omitting the break statement is to run a series of expressions.
For example, if we have:
Then we get the following values depending on what value we set foo to:
foois 0 —'So How Old Are You?'foois 1—'How Old Are You?'foois 2—'Are You?'foois 3—'You?'foois 4—'?'foois 5—'!'foois anything else —‘Please pick a number from 0 to 5!’
Block-Scope Variable in Switch Statements
witch StatementsWe can only have one let or const expression with the same name. Otherwise, we’ll get an error.
For example, if we have:
We’ll get ‘name is already declared.’ The case clauses aren’t a block if it isn’t enclosed in braces, but the switch block is a block.
We can fix this by making the case and default clauses blocks as follows:
Once we enclose it, then we can have let and const declarations with the same name.
Conclusion
The switch statement is a block that we can use to check against different values and do different things according to the value of the expression.
It’s the same as a series of if...else statements with conditions of comparing equality of a value in each.
We can also have multiple case expressions in one case clause to check multiple expressions.
The default clause is for doing something in case that nothing in the case clauses match.
If we want to declare block-scoped variables with let and const with the same name in case and default clauses, we can convert them to blocks by enclosing them with braces.
Last updated
Was this helpful?