JavaScript Switch Statement

The objective of a switch statement is to give an expression to evaluate and several different statements to execute based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, a default condition will be used.
The break statements indicate the end of a particular case. If they were omitted, the interpreter would continue executing each statement in each of the following cases.

Syntax

switch(expression) 
{
    case n:
        code block
        break;
    case n:
        code block
        break;
    default:
        default code block
}

Example

Switch case is explained using the below example.

<html>
<body>

<h1>JavaScript Switch Statement</h1>

<p id="demo"></p>

<script>
var text;
switch (new Date().getDay()) {
    case 0:
        text = "Today is Sunday";
        break;
    default:
        text = "Today is not Sunday";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>

<br>
<p>Powered by easycode.onewebapp.com</p>
</html>

 

Output

JavaScript Switch Statement