E2EHIRING Logo
Jobs
Jobs
courses
Courses
mentorship
Mentorship
more
Moredropdown
E2EHIRING Logo
more
Jobs
Jobs
courses
Courses
mentorship
Mentorship
HomeSepratorIconBlogsSepratorIconJavaScript Switch Conditional StatementSepratorIcon

JavaScript Switch Conditional Statement

Han SoloPriyanka Siddalingreddy
calendar29 Jun 2022
poster

JavaScript Switch Conditional Statement

The switch statement is used to perform different actions based on different conditions. The switch statement is used to select one of many code blocks to be executed. The switch statement evaluates an expression, compares its result with case values, and executes the statement associated with the matching case value.

Syntax

switch (expression) {
   case value1 :
        statement1;
        break;
   case value2:
        statement2;
        break;
   case value3:
        statement3;
        break;
   default:
       statement;
}

How it works.

  • First, evaluate the expression inside the parentheses after the switch keyword.
  • Second, compare the result of the expression with the value1, value2, … in the case branches from top to bottom. The switch statement uses strict comparison (===).
  • Third, execute the statement in the case branch where the result of the expression equals the value that follows the case keyword. The break statement exits the switch statement. If you skip the break statement, the code execution falls through the original case branch into the next one. If the result of the expression does not strictly equal any value, the switch statement will execute the statement in the default branch. 
  • That the switch statement will stop comparing the expression's result with the remaining case values as long as it finds a match.

The switch statement is like the if, else if statement. But it has more readable syntax.

The following example uses the switch statement to get the day of the week based on a day number:

let day = 3;
let dayName;
switch (day) {
  case 1:
    dayName = 'Sunday';
    break;
  case 2:
    dayName = 'Monday';
    break;
  case 3:
    dayName = 'Tuesday';
    break;
  case 4:
    dayName = 'Wednesday';
    break;
  case 5:
    dayName = 'Thursday';
    break;
  case 6:
    dayName = 'Friday';
    break;
  case 7:
    dayName = 'Saturday';
    break;
  default:
    dayName = 'Invalid day';
}
console.log(dayName); //Tuesday

How it works.

First, declare the day variable that holds the day number and the day name variable (dayName).

Second, get the day of the week based on the day number using the switch statement. If the day is 1, the day of the week is Sunday. If the day is 2, the day of the week is Monday, and so on.

Third, output the day of the week to the console.


Conclusion:

  • The switch statement evaluates an expression, compares its result with case values, and executes the statement associated with the matching case.
  • Use the switch statement rather than a complex if, else if statement to make the code more readable.
  • The switch statement uses the strict comparison (===) to compare the expression with the case values.




Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML

copycopycopycopy

Han Solo

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML