Hello friends, I hope you all are doing great. In today’s tutorial, we will have a look at the detailed “Switch Statement in JavaScript”. It’s our 6th tutorial in JavaScript Series. As we have learned about the conditional statement and its types (if, if..else, If...else if) in the last article, a conditional statement is the most useful and common feature in the programming language. Switch is also a type of conditional statement and it will evaluate an expression against multiple possible cases and run one or more blocks of codes base on matching cases. The switched statement work almost as if...else if statement containing many blocks, and it does work so more efficiently than repeated if...else if statements.
Switch Statement in JavaScript
The switch statement is used to perform different action based on multiple cases. How does switch statement work?
Firstly, evaluate the expression once.
Value of expression will be compared with each case.
If there would be a match, the associated block of code will execute.
Syntax switch (expression) { case x: // case x code block (statement x) break;case y: // case y code block (statement y)break;default: // execute default code block (statement default)}
Break Keyword in JavaScript Switch Statement
As the name says break, When JavaScript reaches the break keyword, it breaks out of the switch block. It will only stop the execution of inside the block. It will continue until reaching the last break case. Eventually, reach the last case and the break keyword will end the switch block.
Default Keyword in JavaScript Switch Statement
If there is no case match then the default code block will run. There could be only one default keyword in the switch statement. The default keyword is optional, but it is recommended that you use it.Let’s see an example of a switch statement. Firstly, the expression will be evaluated. The first case, 3 will be tested against the expression. The code will not execute because it does not match the expression and it will be skipped.Then case 4 will be tested against the expression. Since 4 matches the expression, the code will execute and exit out of the switch block. And your output will be displayed like this.OutputLet’s make another example of a working switch. In this example, we will find the current day of the week with the newDate()method and getDay()to print a number equivalent to the current day. 0 stand for Sunday, 1 stand for Monday and then throughout till Saturday with number 6.OutPutThis code was tested on Monday that’s why it is displaying Monday. But your output would be change depending on the day you are testing. I have added default too in case of any error.
Multiple Cases in JavaScript Switch Statement
There is a chance that you may face a code in which you have to display the same output for multiple cases.So, we have made an example for you to understand these multiple cases. In this example first, the new Date() method will find a number compared to the current month and then apply the month variable.OutputIn today tutorial, we review about the switch, and how it is similar to if..else if statement. We reviewed the switch statement with three different examples. To learn more about JavaScript you can read these article series from the start. I am sure these will help you and if you have any question regarding this article. You can comment down below and ask anything. Thank you so much for reading this article and we are doing our best to deliver you our best. Please visit again. See you in the next tutorial.
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected
Comments on ‘’ Switch Statement in JavaScript ‘’ ( 0 )