over 9 years ago
The JavaScript switch statement is used to execute one code from multiple expressions. It is just like else if statement. But it is convenient than if..else..if because it can be used with numbers, characters etc.
Syntax:
- switch(expression){
- case value1:
- code to be executed;
- break;
- case value2:
- code to be executed;
- break;
- ......
- default:
- code to be executed if above values are not matched;
- }
switch(expression){
case value1:
code to be executed;
break;
case value2:
code to be executed;
break;
...... default: code to be executed if above values are not matched; }
- <!DOCTYPE html>
- <html>
- <body>
- <script>
- var grade='B';
- var result;
- switch(grade){
- case 'A':
- result="A Grade";
- break;
- case 'B':
- result="B Grade";
- break;
- case 'C':
- result="C Grade";
- break;
- default:
- result="No Grade";
- }
<!DOCTYPE html> <html> <body> <script> var grade='B'; var result; switch(grade){ case 'A': result="A Grade"; break; case 'B': result="B Grade"; break; case 'C': result="C Grade"; break; default: result="No Grade"; }
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)