Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript switch statement


May 06, 2021 JavaScript


Table of contents


JavaScript switch statement


Switch statements are used to perform different actions based on different conditions.


JavaScript switch statement

Use the switch statement to select one of the more than one block of code to execute. You can learn how to use switch statements for multi-option selection in JavaScript programming.

Grammar

switch(n)
{
case 1:
Execute code block 1
break;
case 2:
Execute code block 2
break;
default:
Code performed with Case 1 and Case 2
}

Code interpretation:

  • The switch expression is evaluated once
  • Compare the value of the expression with the value of each case
  • If there is a match, the associated code is executed

How it works: First set the expression n (usually a variable). T he value of the expression is then compared to the value of each case in the structure. I f there is a match, the block of code associated with the case is executed. Use break prevent code from automatically running to the next case.

Show the name of today's week. Please note that Sunday is 0, Monday is 1, Tuesday is 2, and so on:

var day=new Date().getDay();
switch (day)
{
case 0:
x="Today it's Sunday";
break;
case 1:
x="Today it's Monday";
break;
case 2:
x="Today it's Tuesday";
break;
case 3:
x="Today it's Wednesday";
break;
case 4:
x="Today it's Thursday";
break;
case 5:
x="Today it's Friday";
break;
case 6:
x="Today it's Saturday";
break;
}

The result of x's operation:


Try it out . . .

Break keywords

If JavaScript encounters break keyword, it jumps out of the switch block.

This will stop the execution of more code in the block as well as case

If a match is found and the task is completed, execution is interrupted at random. No more testing is required.

break saves a lot of execution time because it "ignores" the execution of other code in the switch block.

You don't have to interrupt the last case in the switch case The block of code ends naturally here.

Default keywords

Use the default keyword to specify what to do if the match does not exist:

If today is not Saturday or Sunday, the default message is output:

var day=new Date().getDay();
switch (day)
{
case 6:
x="Today it's Saturday";
break;
case 0:
x="Today it's Sunday";
break;
default:
x="Looking forward to the Weekend";
}

The result of x's operation:


Try it out . . .

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

switch (new Date().getDay()) {

default:

TEXT = "Looking forward to weekends!"

break;

case 6:

TEXT = "Today is Saturday";

break;

case 0:

TEXT = "Today is Sunday";

}


Try it out . . .

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

Common blocks of code

Sometimes you need a different case to use the same code.

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

switch (new Date().getDay()) {

case 4:

case 5:

Text = "Weekend is coming :)"

break;

case 0:

case 6:

TEXT = "Today is weekend ~";

break;

default:

TEXT = "Looking forward to weekends!"

}


Try it out . . .

The details of Between

If multiple casees match a case value, select the first case.

If no matching case is found, the program continues to use the default label.

If the default label is not found, the program continues with the statement after switch.

Strict comparison

Switch case uses a strict comparison.

The value must be the same as the type you want to match.

Strict comparisons can only be true if the operans belong to the same type.

In this example, x will not match:

var x = "0";

switch (x) {

case 0:

text = "Off";

break;

case 1:

text = "On";

break;

default:

text = "No value found";

}


Try it out . . .

Related articles

Easy to learn JavaScript: JavaScript Switch Case