Switch Statment in JavaScript javascript switch multiple case javascript switch case range javascript switch multiple case javascript switch return javascript switch case multiple expressions switch vs if else performanceHello 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. Switch Statment in JavaScript javascript switch multiple case javascript switch case range javascript switch multiple case javascript switch return javascript switch case multiple expressions switch vs if else performance

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.

Output

Switch Statment in JavaScript javascript switch multiple case javascript switch case range javascript switch multiple case javascript switch return javascript switch case multiple expressions switch vs if else performanceLet’s make another example of a working switch. In this example, we will find the current day of the week with the new Date()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.

Switch Statment in JavaScript javascript switch multiple case javascript switch case range javascript switch multiple case javascript switch return javascript switch case multiple expressions switch vs if else performance

OutPut

This 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. 

Switch Statment in JavaScript javascript switch multiple case javascript switch case range javascript switch multiple case javascript switch return javascript switch case multiple expressions switch vs if else performance

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. 

Switch Statment in JavaScript javascript switch multiple case javascript switch case range javascript switch multiple case javascript switch return javascript switch case multiple expressions switch vs if else performance

In this example first, the new Date() method will find a number compared to the current month and then apply the month variable.

Output

Switch Statment in JavaScript javascript switch multiple case javascript switch case range javascript switch multiple case javascript switch return javascript switch case multiple expressions switch vs if else performanceIn 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment