MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Switch statement

Switch statements compare the value of an expression against 1 or more values and executes different sections of code based on that comparison.


var value = 1;
switch (value) {
 case 1:
 console.log('I will always run');
 break;
 case 2:
 console.log('I will never run');
 break;
}

Output

"I will always run"

The break statement "breaks" out of the switch statement and ensures no more code within the switch statement is executed. This is how sections are defined and allows the user to make "fall through" cases.


switch (value) {
 case 1:
 console.log('I will only run if value === 1');
 // Here, the code "falls through" and will run the code under case 2
 case 2:
 console.log('I will run if value === 1 or value === 2');
 break;
 case 3:
 console.log('I will only run if value === 3');
 break;
}

Output

'I will only run if value === 1'
'I will run if value === 1 or value === 2'

The last case is the default case. This one will run if no other matches were made.


var animal = 'Lion';
switch (animal) {
 case 'Dog':
 console.log('I will not run since animal !== "Dog"');
 break;
 case 'Cat':
 console.log('I will not run since animal !== "Cat"');
 break;
 default:
 console.log('I will run since animal does not match any other case');
}

Output

'I will run since animal does not match any other case'

It should be noted that a case expression can be any kind of Expression. This means you can use comparisons, function calls, etc. as case values.


function john() {
 return 'John';
}
function jacob() {
 return 'Jacob';
}
switch (name) {
 case john(): // Compare name with the return value of john() (name == "John")
 console.log('I will run if name === "John"');
 break;
 case 'Ja' + 'ne': // Concatenate the strings together then compare (name == "Jane")
 console.log('I will run if name === "Jane"');
 break;
 case john() + ' ' + jacob() + ' Jingleheimer Schmidt':
 console.log('His name is equal to name too!');
 break;
}

Multiple Inclusive Criteria for Cases

Since cases "fall through" without a break or return statement, you can use this to create multiple inclusive criteria:


var x = "c"
switch (x) {
 case "a":
 case "b":
 case "c":
 console.log("Either a, b, or c was selected.");
 break;
 case "d":
 console.log("Only d was selected.");
 break;
 default:
 console.log("No case was matched.");
 break; // precautionary break if case order changes
}

Output

"Either a, b, or c was selected."


Conclusion

In this page (written and validated by ) you learned about Javascript Switch statement . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript If Else-If Else Control.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.