Using the Switch Statement in JavaScript

You can use the switch statement to select a particular group of statements to be executed among several other groups of statement.

Demo

Let’s create a document called switchStatement.html:



Write the following code:



Execute the script by Opening the file in the Web Browser. If you are using Internet Explore click on “Allow Blocked Content” to allow the script to execute and you are using Mozilla Firefox then click on allow “ActiveX Controls”.

Explanation

letter” is the expression of switch statement. The value of letter variable is compared with the case values one by one. When a match is found, which in this case is the third case statement, the set of statements specified in the case is executed.

Thank you for reading this blog.

Ebook Download
View all
Learn
View all