When to use a Switch Statement?
Switch statements are an alternative to if-else statements. Following is the list of differences and similarities between the switch and if-else statements:
- For an, if statement, the condition can be a range value, whereas a switch statement can only take a single integer, string object, or enumerated value as its condition.
- If-else statements are great for boolean conditions, whereas a switch statement works with fixed data values.
- The condition in an if-else statement can be an equality or any other logical expression, whereas a switch statement can only work with equalities.
Note: We will use the browser console to demonstrate examples performed in this post. To open up the browser console:
- Use the F12 key in Chrome and other chromium-based browsers.
- Use CTRL + SHIFT + K keyboard shortcut keys for Mozilla.
- Use Option + ⌘ + C keyboard shortcut keys in Safari (if developer menu does not appear, then open Preferences by pressing ⌘ +, and in Advanced tab check “Show Develop menu in menu bar”).
How to use a switch statement?
The switch statement evaluates the variable or the expression present between the parentheses (). It compares it with each case present in the switch statements body. If the first case is true, then the statements present in its body are executed. If the second case is true, then the statements present in the second case’s body are executed. The break and default statements are optional. The statements present under default are only executed in case all the case statements are false. The break statement is used to exit the body of the switch statement once a case is matched with the expression. If we do not use a break statement, then the switch statement will evaluate the expression against every case even if one of the cases is true.
console.log("Car Name: Toyota Prius\nFuel Economy: 18/21 KM/L");
console.log("Car Name: Toyota Vitz\nFuel Economy: 20/22 KM/L");
console.log("Car Name: Toyota Corolla(Axio)\nFuel Economy: 19/22 KM/L");
console.log("This car is not present in our Database.")
In the example given above, first, we declared a variable named car and assigned it a value. Next, we passed the variable car as an argument to the switch statement and gave it three cases. The switch statement will check the value of the variable car against each case until a match is found. Then it will execute the body of that particular case and then terminate the switch statement using the break statement.
In the example above, as the value of the variable car is ‘Toyota Prius,’ the switch statement only checks against the first case, and the switch statement is terminated. But if we modify the value of the variable car in the following way:
Then the switch statement will check against every case, and the output will be:
If we put the name of any other car which is not present as a case in our switch statement, then the body of the default statement will execute, and the output will change to:
Switch statements are used to check multiple conditions on a single variable. Switch statements are an efficient alternative to if-else statements. If statements can evaluate almost all types of data while switch statements can only evaluate integers or characters. You can use them in your code, depending on your style.
In this post, we have learned what switch statements are. Moreover, we also learned about when we should prefer them over other conditional statements.