Use of Switch Statement in JavaScript

In this article I have described the Switch which is a conditional statement used in JavaScript.
  • 1804

JavaScript Switch Statement

Instead of multiple "if...else if" statements we prefer to use Switch statement for programming point of view.

Syntax

switch (expression)

{
        case condition 1: JavaScript statement(s)
        break;
        case condition 2: JavaScript statement(s)
        break;
        ...
        case condition n: JavaScript statement(s)
        break;
        default: statement (s)
}

Example

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Switch statement in JavaScript</title>

    <script type="text/javascript">

        var movie = 'A';

        document.write("Start of switch block<br />");

        switch (movie) {

            case 'A': document.write("AWESOME<br />");

                break;

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

                break;

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

                break;

            case 'D': document.write("GOOD<br />");

                break;

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

                break;

            default: document.write("Unknown movie<br />")

        }

        document.write("End of switch block");

</script>

</head>

</html>

OUTPUT
s.jpg

Further Readings

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2018 DotNetHeaven. All rights reserved.