Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Switch

JS Switch Statement

The switch statement is used to perform different actions based on different conditions.

Syntax
					 
        
          switch(expression) {
            case x:
              // code block
              break;
            case y:
              // code block
              break;
            default:
              // code block
          }
        
      
Working

The switch expression is evaluated once.

The value of the expression is compared with the values of each case.

If there is a match, the associated block of code is executed.

If there is no match, the default code block is executed.

					 
        
          switch (new Date().getDay()) {
            case 0:
              day = "Sunday";
              break;
            case 1:
              day = "Monday";
              break;
            case 2:
               day = "Tuesday";
              break;
            case 3:
              day = "Wednesday";
              break;
            case 4:
              day = "Thursday";
              break;
            case 5:
              day = "Friday";
              break;
            case 6:
              day = "Saturday";
          }
        
      
The break Keyword

When JavaScript reaches a break keyword, it breaks out of the switch block.

This will stop the execution inside the switch block.

If you omit the break statement, the next case will be executed even if the evaluation does not match the case.

It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.

The default Keyword

The default keyword specifies the code to run if there is no case match.

					 
        
          switch (new Date().getDay()) {
            case 6:
              text = "Today is Saturday";
              break;
            case 0:
              text = "Today is Sunday";
              break;
            default:
              text = "Looking forward to the Weekend";
          }
        
      

The default case does not have to be the last case in a switch block.

If default is not the last case in the switch block, remember to end the default case with a break.

					 
        
          switch (new Date().getDay()) {
            default:
              text = "Looking forward to the Weekend";
              break;
            case 6:
              text = "Today is Saturday";
              break;
            case 0:
              text = "Today is Sunday";
          }
        
      
Common Code Blocks

Sometimes you will want different switch cases to use the same code.

In this example case 4 and 5 share the same code block, and 0 and 6 share another code block.

					 
        
          switch (new Date().getDay()) {
            case 4:
            case 5:
              text = "Soon it is Weekend";
              break;
            case 0:
            case 6:
              text = "It is Weekend";
              break;
            default:
              text = "Looking forward to the Weekend";
          }
        
      
Switching Details

If multiple cases matches a case value, the first case is selected.

If no matching cases are found, the program continues to the default label.

If no default label is found, the program continues to the statement(s) after the switch.

Strict Comparison

Switch cases use strict comparison (===).

The values must be of the same type to match.

A strict comparison can only be true if the operands are of the same type.

					 
        
          let x = "0";
          switch (x) {
            case 0:
              text = "Off";
              break;
            case 1:
              text = "On";
              break;
            default:
              text = "No value found";
          }
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved