To execute a single block of code satisfying a condition in case statement of JavaScript switch-case is used. break keyword is used in every case statement to exit the switch statement so that it should not enter into other case clause statement.

Syntax

switch (expression)
{
    case value1:
    // code block to be executed when expression result matches value1;
    [break;]

    case value2:
    // code block to be executed when expression result matches value2;
    [break;]

    ...

    case valueN:
    // code block to be executed when expression result matches valueN;
    [break;]

    [default:
    // code block to be executed when expression result doesn't match any of the values.
    [break;]]
}

expression: an expression whose result is matched in each switch case clause.

case valueN: a case clause keeps on matching the result of expression with each case clause until it matches the value. It executes the code statements where the case value matches until either it reaches the end of the switch statement block or a break.

default: the code statements inside the default clause are executed if expression result doesn’t match any of the case clauses.

Usually break is not required in default case as it automatically exits after default.

Example

var n;
n = (Math.random() * 10);
n = Math.ceil(n);

switch(n)
{
    case 5:
           document.write('Random Number is 5');
           break;
 
    case 6:
           document.write('Random Number is 6');
           break;
 
    case 7:
           document.write('Random Number is 7');
           break;
 
    case 8:
           document.write('Random Number is 8');
           break;
 
    case 9:
           document.write('Random Number is 9');
           break;

    case 10:
            document.write('Random Number is 10');
            break;

    default:
            document.write('Random Number is less than 5');
}

Above example is a code for a simple JavaScript game to trace the random number greater than 5 using JavaScript switch-case statements.

No comments yet.

Leave a Comment

All fields are required. Your email address will not be published.

Recent JavaScript Tutorials

JavaScript if-else Ternary Operator

The JavaScript conditional ternary operator is considered as shorthand for if-else statement that is frequently used to return the result in single code statement. It takes three operands to return ...

JavaScript do…while Loop

do...while loop in JavaScript works like while loop in JavaScript the only difference is that do block executes at least once before while loop’s test condition. That’s why do block ...

JavaScript while Loop

while loop in JavaScript works same like for loop in JavaScript but in a little bit different manner. while loop runs until the condition is true and exits when the ...

JavaScript for Loop

for loop in JavaScript is most often used to execute a block of code with different value each time. So, to reduce the code length instead of adding number of ...

JavaScript switch-case Statement

To execute a single block of code satisfying a condition in case statement of JavaScript switch-case is used. break keyword is used in every case statement to exit the switch ...

Recent Comments