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…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

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.