Switch Case command in JavaScript

You can use multiple if . else . if statements as in the previous post to make a branch of multiple choices. However, it is not always the best solution, especially when all branches depend on the value of a single variable.

Starting with JavaScript 1.2, you can use a switch command to correctly handle this situation, and it's actually more efficient than repeating if . else if statements .

Implementation diagram

The following diagram explains the switch-case command to work:

Switch Case command in JavaScript Picture 1

Syntax

The goal of a switch command is to provide an expression to estimate and several different commands to execute on the value of the expression. The interpreter checks each case - the case with the value of the expression until a match is found. If there is no match, a default condition - will be used by default .

 switch ( expression ) { case condition 1 : statement ( s ) break ; case condition 2 : statement ( s ) break ; . case condition n : statement ( s ) break ; default : statement ( s ) } 

Break command instructs the end of each specific case. If they are omitted, the interpreter will continue to execute each instruction in each of the following cases.

We will discuss the break command in the Loop control chapter.

For example

Try the following example to execute the switch-case command

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result:

 Entering block switch 
Good job
Block Exiting switch
Đặt biến vào giá trị khác nhau và thử thử .

Break commands play an important role in switch-case commands. Try the following code using the switch-case command without any break commands.

  type = "text/javascript" >  
 Set the variable to different value and then try. 

Result:

 Entering block switch 
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Block Exiting switch
Đặt biến vào giá trị khác nhau và thử thử .

According to Tutorialspoint

Previous lesson: if . else command in JavaScript

Next: The while loop in JavaScript

4 ★ | 1 Vote

May be interested

  • The while loop in JavaScriptPhoto of The while loop in JavaScript
    while writing a program, you may encounter a situation where you need to perform an iterative action. in these situations, you will need to write loop commands to reduce the number of lines of code.
  • For ... loop in JavaScriptPhoto of For ... loop in JavaScript
    the for ... in loop is used to iterate over the properties of an object. when we haven't discussed the object yet, you may not feel comfortable with this loop. but once you understand how objects work in javascript, you will find this loop very useful.
  • Loop control in JavaScriptPhoto of Loop control in JavaScript
    javascript provides high control to handle loop commands and switch commands. there may be a situation when you need to exit the loop without having to go to its endpoint. there may also be situations when you want to jump over a part of the code block and start the next loop.
  • JavaScript functionsPhoto of JavaScript functions
    a function is a group of reusable code that can be called anywhere in your program. this makes it unnecessary to write the same code over and over again. it helps programmers write modular code. functions allow a programmer to divide a large program into small and manageable functions.
  • Event (Event) in JavaScriptPhoto of Event (Event) in JavaScript
    javascript interaction with hmtl is handled through events that occur when the user or browser manipulates a page.
  • Page navigation (Redirect) in JavaScriptPhoto of Page navigation (Redirect) in JavaScript
    you may have a situation when you click on a url to go to page x but you are directed to page y. it happens because page redirection - redirects the page. this concept is different from: javascript - refresh page.