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.

You might also like…

Scriptcase uses the MVC template?

Maybe you might have thought about this at some point, “Scriptcase uses the MVC templates?" Well, ...

How to implement A2F using Own Development in NodeJS in a Scriptcase application

A2F using Own Development in NodeJS A2F: In a previous article, we talked about Swivel as an extern...

Specialists or generalists, who gets a better advantage by using Scriptcase?

Hey there! Today  we will figure what are the ideal profiles for those who wish to take advantage o...

Comment this post

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.