Switch Case using JavaScript

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.


  <script type = "text/javascript">

    var grade = 'B';
    document.write("Entering switch block<br />");
    switch (grade)
        case 'A': document.write("Good job<br />");

        case 'B': document.write("Pretty good<br />");

        case 'C': document.write("Passed<br />");

        case 'D': document.write("Not so good<br />");

        case 'F': document.write("Failed<br />");

        default: document.write("Unknown grade<br />")
    document.write("Exiting switch block");


  <p>Set the variable to different value and then try...</p>

Leave a Reply

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