JavaScript's switchstatement allows you to specify different actions to be taken based on different values of a variable or expression. Whereas the if statement allows only two alternatives, for the case where an expression is either true or false, switch allows multiple alternatives.
- Difficulty:
- Moderate
Instructions
- JavaScript or text editor
-
-
1
Enter the skeleton for the switch statement using the following syntax. The expression can be a single variable or a formula.switch (expression) { }
-
2
Place one or more case statements inside the curly brackets following the switch statement The value given following the word case will often be a constant, such as 1 or "red", but could be any variable or expression. The statements will be executed if the value of the expression given in the switch statement matches the value following the case statement.
case value: statement(s);
-
3
Insert a break statement following the statement or statements in the case command. If you don't, the statements for the next case will be executed, which is probably not what you want.
-
4
Place an optional default statement after all the cases. The statements in the default statement will be executed if the value of the expression given in the switch statement does not match any of the values following the case statements above. If you omit the default statement, and the expression given in the switch statement does not match any of the values following the case statements, then nothing will be done.
Read Next:
-
Java Switch Statement Tutorial
The Switch statement in Java is an alternative to having multiple "if" and "else if" statements chained one after another where the...
-
Conditional Statement Usage in Java Script
A "switch" statement takes the place of multiple "if" statements. A "switch" statement compares the value of the expression and looks for...
-
How to Create a Switch Statement in C
Use the Switch statement in C to evaluate a large number of values for a single variable or expression. Switch can be...
-
How to Use a Switch Statement
The switch statement is used in programming languages such as C, C++, Javascript and Java. When you desire to code a logical...
-
How to Use the Break Statement in JavaScript
Code a break statement following the statements after a case statement inside a switch statement. Otherwise, execution will continue with the statements...
-
How to Use a Switch Statement in C
The switch statement is a powerful programming structure that exists in most programming languages. It will allow you to write a section...
-
How to Create an If Else Statement in C
The If Else statement is the single most important program construct in C, and since you will create and use it in...
-
How to Use a Switch Case
The switch-case statement in an important tool for cleanly controlling the flow of a program. It functions identically to a long list...
-
How to Use the Continue Statement in JavaScript
The JavaScript continue statement offers an easy way to stop the current iteration of a loop and start a new one. In...
-
Array Javascript Tutorial
Combine two arrays together with the "concat" command from the example below. The "concat" command takes at least two parameters, but can...
-
How to Write a Case Statement in C++
As computer programs are not limited to the linear or sequential execution of commands, programming language constructs support run time branching of...
-
How to Use Switch Case in C#
Using "If...else" throughout your C# application can be hard to read and may also lead to you making programming mistakes. Instead use...
-
How to Make a Fabric Switch Plate
Fabric light switch plates are a fun way to add a pop of unexpected color to a room or to liven up...
-
How to Change Styles with Javascript
Cascading style sheets (CSS) provide a quick and easy way to specify the look and feel of documents written in a markup...
-
How to Create a While Loop in JavaScript
JavaScript, like all programming languages, supports loops or repetition, in its case with the for statement. The for statement in JavaScript comes...
-
How to Switch the Values of Two Variables Without Using a Temporary Variable
In programming, there are many times that you need to switch two variable values (such as in a Bubble Sort). Most people...
-
How to Enable JavaScript in IE8
JavaScript is a type of programming language used in websites around the world. Without JavaScript enabled in your web browser, you may...
-
How to Change Browser Settings Using Javascript
JavaScript is a programming language that is included in webpages to improve functionality and the display of webpages. It allows the integration...