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.

<html>
<body>

  <script type = "text/javascript">

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

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

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

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

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

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

  </script>

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

Leave a Reply

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