In JavaScript programming language it works the same as the IF-ELSE statement. In Switch Case in JavaScript, the values that give the true result is allowed to execute the piece of the code, and the rest remains still.

Switch case cases are the parallels of long If-Else statements in JavaScript. The switch case works the same as the code that works in the If-Else statement in JavaScript. In the If-Else statement the code where the condition is satisfied, the code under the satisfied condition is executed the other conditions are left unexecuted as it is. The same is the case with Switch Case in JavaScript.

Below is the simple syntax of Switch case in jJvaScript.

switch(expression) {

  case 1:

    // piece of code

    break;

  case 2:

    // piece of code

    break;

  default:

    // piece of code

}

In switch case statements in JavaScript, the value is passed to the switch in expression and it checks that where does the value lies in the case. If it gets the value in the case it directly switches the case to that line where the case is coded and if it is unable to find the required expression that it simply goes to the default in switch case.

Let’s just understand it with a simple example. We want to print the days of a week with the specified value passed against it. It can be done using If-Else statement as well beacuse We know switch case statement are same as If-Else statements but switch case statement allow us to do the same work in very short piece of code. We are going to implement above example using switch case statement.

Below is the simple syntax of Switch case in jJvaScript.

x=3

//The value of x will be passed as an expression to switch and it will check where does the case lie thus it

//switches the case to the specified case

switch(x) {

// The value 3 is passed to switch so it will directly go to case 3

  case 1:

    console.log(“Today is Monday“)// piece of code

    break;

  case 2:

    console.log(“Today is Tuesday”)// piece of code

    break;


  case 3:

    console.log(“Today is Wednesday”)// piece of code

    break;


  case 4:

    console.log(“Today is Thursday”)// piece of code

    break;


  case 5:

    console.log(“Today is Friday”)// piece of code

    break;


  case 6:

    console.log(“Today is Saturday”)// piece of code

    break;


  case 7:

    console.log(“Today is Sunday”)// piece of code

    break;

  default:

   console.log(“Unspecified number selected”) // piece of code

}

Output
Today is Wednesday

Let’s go with another example

x=7

//The value of x will be passed as an expression to switch and it will check where does the case lie thus it

//switches the case to the specified case

switch(x) {

// The value 7 is passed to switch so it will directly go to case 3

  case 1:

    console.log(“Today is Monday“)// piece of code

    break;

  case 2:

    console.log(“Today is Tuesday”)// piece of code

    break;


  case 3:

    console.log(“Today is Wednesday”)// piece of code

    break;


  case 4:

    console.log(“Today is Thursday”)// piece of code

    break;


  case 5:

    console.log(“Today is Friday”)// piece of code

    break;


  case 6:

    console.log(“Today is Saturday”)// piece of code

    break;


  case 7:

    console.log(“Today is Sunday”)// piece of code

    break;

  default:

   console.log(“Unspecified number selected”) // piece of code

}

Output

Today is Sunday

Let’s do a short quick example for more clear attitude

geometry=”Sharpner”

switch(geometry)

{

case “Pencil”:

console.log(“This is a pencil”)

break;

case “Pen”:

console.log(“This is a Pen”)

break;

default:

console.log(“Nothing was found”)

}

Output

Nothing was found

RECOMMENDED FOR YOU

Leave a Reply

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