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]:

[code]

break;

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”);
break;
case 1:
document.write (“February”);
break;
case 2:
document.write (“March”);
break;
case 3:
document.write (“April”);
break;
case 4:
document.write (“May”);
break;
case 5:
document.write (“June”);
break;
case 6:
document.write (“July”);
break;
case 7:
document.write (“August”);
break;
case 8:
document.write (“September”);
break;
case 9:
document.write (“October”);
break;
case 10:
document.write (“November”);
break;
default:
document.write (“December”);
}
//–>
</SCRIPT>

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

javascript

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…

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...

Disabling autofill and autocomplete Chrome browser in login screens

Hello, here I want to show an alternative to disable the autofill and autocomplete of the chrome bro...

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 sales@scriptcase.net. Learn more about our Privacy Police.