JavaScript Switch
JavaScript Basics

JavaScript Switch

The JavaScript Switch Statement

The switch statement allows you to execute one of many code blocks based on a specific condition.

Syntax

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

How it Works:

  1. The switch expression is evaluated once.
  2. The value of the expression is compared with the values of each case.
  3. If there is a match, the associated block of code is executed.
  4. If there is no match, the default code block is executed.

Example

Display a message based on the current month:

switch (new Date().getMonth()) {
  case 0:
    month = "January";
    break;
  case 1:
    month = "February";
    break;
  case 2:
    month = "March";
    break;
  case 3:
    month = "April";
    break;
  case 4:
    month = "May";
    break;
  case 5:
    month = "June";
    break;
  case 6:
    month = "July";
    break;
  case 7:
    month = "August";
    break;
  case 8:
    month = "September";
    break;
  case 9:
    month = "October";
    break;
  case 10:
    month = "November";
    break;
  case 11:
    month = "December";
    break;
  default:
    month = "Unknown month";
}

he result of month will be based on the current month.

The break Keyword

When JavaScript encounters a break keyword, it exits the switch block. This stops the execution inside the switch block. It is not necessary to break the last case in a switch block as it ends there anyway.

Note

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

The default Keyword

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

Example

Display a message based on the current month, with a default message if the month is unknown:

switch (new Date().getMonth()) {  case 11:    text = "It's December";    break;  case 0:    text = "It's January";    break;  default:    text = "It's a great month!";}‍

The result of text will depend on the current month.

Note

The default case does not have to be the last case in a switch block. If default is not the last case, remember to end it with a break.

Common Code Blocks

Sometimes, different switch cases share the same code block. For instance, if you want to display a message for the weekend:

Example

switch (new Date().getDay()) {  case 5:  case 6:    text = "It's the Weekend!";    break;  default:    text = "Looking forward to the Weekend";}

Switching Details

  1. If multiple cases match a case value, the first case is selected.
  2. If no matching cases are found, the program continues to the default label.
  3. 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.

Example

There will be no match for x because the type is different:

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

The result of text will be "No value found" because x is a string, not a number.

Take a look into your desired course