3.5 Switch Statement
5️⃣

3.5 Switch Statement

3.5 Switch Statement

What Is the Switch Statement?

The switch statement is used to perform different actions based on different conditions. It really is similar to the a if-else statement.

Basic Structure

Use the switch statement to select one of the many code blocks to be executed.

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

Here's a run down on how this works:

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

Components

break Keyword

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

This will stop the execution of the code clock between the case and break.

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

⚠️

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

default Keyword

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

Example

The currentDay returns the weekday as a number between 0 and 6.

If today is neither Saturday (6) nor Sunday (0), text will have a message of "Looking forward to the weekend".

let currentDay = 5;

switch (currentDay) {
  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 Full Example

The currentDay returns the weekday as a number between 0 and 6 (Sunday = 0, Monday = 1, Tuesday = 2, ...).

This example uses the weekday number to calculate the weekday name:

let currentDay = 0;

switch (currentDay) {
  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";
}

Here day will have the value: 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 (currentDay) {
  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";
}

switch Details

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

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

If no default case 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. In this example, there will be no match for x:

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

// text = "No value found"

Previous Section

4️⃣
3.4 Ternary Operator

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.