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…

What advantages for a company in adopting a low-code tool?

Let's understand how low-code tools have become a facilitator in the web development process as they...

How to implement A2F using Swivel in a Scriptcase application

I want to start talking about Swivel, Swivel is a provider of authentication solutions who have been...

Comments, tips and interactions inside Scriptcase.

Hello, here are some tips, comments and interactions about database and tools like HTML, CSS, JavaSc...

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.