The structure SWITCH is used in many programming languages ​​and Javascript is no different. Thanks to it is possible to trigger certain actions from the value of a variable. Thus, the code is much leaner and also the processing happens much more quickly, but carefully. The case does not accept conditional, should be used as the absolute value of the variable factor verification, if necessary the use of conditionals the best option is to use a structure optimized IFS.

In Javascript function of items remains the same. Starts the command with the syntax switch () passing the variable as a parameter and then immediately create the cases.

The CASE structure have the following structure:

case [value]:



See below an example of how to use the SWITCH, CASE and DEFAULT in Javascript.

SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
document.write (“Month Now: “);
today = new Date();
switch (today.getMonth()) {
case 0:
document.write (“January”);
case 1:
document.write (“February”);
case 2:
document.write (“March”);
case 3:
document.write (“April”);
case 4:
document.write (“May”);
case 5:
document.write (“June”);
case 6:
document.write (“July”);
case 7:
document.write (“August”);
case 8:
document.write (“September”);
case 9:
document.write (“October”);
case 10:
document.write (“November”);
document.write (“December”);

The DEFAULT will be used if none of the previous CASES fulfill the requirement.


Understand the operation of this command is essential for everyday activities of any programmer, so understands the importance of it. Want to test this and other functions, so download now for free Scriptcase and learn not only this, but several other features that will bring more results for your development projects.

By ,

August 8, 2013


You might also like…

5 reasons you can’t miss a Black Friday Scriptcase!

This November we bring you the biggest and most important discounts of the year! And do you know wh...

Taking a look at Scriptcase version 9.7

After the big Scriptcase 9.7 presentation event, we have a new version of Scriptcase for download, ...

October Newsletter

Follow the latest news for the month of October! Webinars, blog posts, videos, and more. WEB...

You might also like…

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at Learn more about our Privacy Police.