In JavaScript, the switch...case statement provides a more concise way to handle multiple conditions compared to using nested if...else statements. It is particularly useful when you have a single expression that needs to be compared against several possible values.
Understanding how to use the switch...case statement effectively can make your code cleaner and easier to read, especially when dealing with multiple conditional branches. In this tutorial, we'll explore how to use the switch...case statement, the importance of the break keyword, and how to handle default cases.
The switch...case statement evaluates an expression and executes the code block associated with the first case whose value matches the expression. If no matching case is found, it executes the code in the default case, if one exists.
Here's the basic syntax:
1switch (expression) {2case value1:3// Code to execute if expression === value14break;5case value2:6// Code to execute if expression === value27break;8default:9// Code to execute if no cases match10}
expression is evaluated once.case label is compared with the result of the expression using strict equality (===).break keyword is used to terminate the switch statement and prevent fall-through to subsequent cases.default block (if present) is executed.Let's see how a simple switch...case statement works:
1let day = "Monday";23switch (day) {4case "Monday":5console.log("It's the start of the week.");6break;7case "Tuesday":8console.log("Just another day.");9break;10case "Wednesday":11console.log("Halfway through the week.");12break;13default:14console.log("It's not a weekday.");15}
It's the start of the week.
You can have multiple cases that execute the same code block:
1let fruit = "apple";23switch (fruit) {4case "banana":5case "orange":6console.log("It's a citrus fruit.");7break;8case "apple":9console.log("It's an apple.");10break;11default:12console.log("Unknown fruit.");13}
It's an apple.
The default case is optional but recommended to handle unexpected values:
1let number = 5;23switch (number) {4case 1:5console.log("One");6break;7case 2:8console.log("Two");9break;10default:11console.log("Other number");12}
Other number
If you forget to include the break keyword, it can lead to fall-through behavior:
1let grade = "B";23switch (grade) {4case "A":5console.log("Excellent!");6case "B":7console.log("Well done.");8case "C":9console.log("You passed.");10default:11console.log("Try harder next time.");12}
Well done. You passed. Try harder next time.
Warning
break keyword at the end of each case to prevent fall-through. This can lead to unexpected behavior if not handled properly.Let's create a practical example where we use a switch...case statement to categorize user input:
1let userInput = prompt("Enter a number between 1 and 3:");23switch (userInput) {4case "1":5console.log("You entered one.");6break;7case "2":8console.log("You entered two.");9break;10case "3":11console.log("You entered three.");12break;13default:14console.log("Invalid input. Please enter a number between 1 and 3.");15}
Tip
prompt dialog in action.switch...case statement is used for multiple conditional branches.===).break keyword to terminate the switch statement and prevent fall-through.default case handles any values that do not match the specified cases.| Concept | Description |
|---|---|
switch...case | Evaluates an expression against multiple cases. |
break | Terminates the switch statement to prevent fall-through. |
default | Handles any values that do not match the specified cases. |
Now that you've learned about the switch...case statement, let's move on to loops in JavaScript. The next topic is the JavaScript for Loop. Understanding loops will help you automate repetitive tasks and make your code more efficient.
Happy coding!