JavaScript Switch Case: The JavaScript Else Statement allows us to choose between TRUE or FALSE. When there are more than two options, we use JavaScript Nested If Statement. Say, What if we have ten alternatives to choose?, if we use Nested If, the programming logic will be difficult to understand. Here, JavaScript Switch statements and Else if can handle these types of problems effectively.
The working functionality of the JavaScript Switch Case is almost the same as if condition. As we said before, the Switch statement may have n number of cases. So, the switch case compares the expression value with the values assigned in the case statements. If both the values (expression and case value) match, then statements present in that case statement will execute. Let us see the syntax of switch case in Javascript for better understanding
JavaScript Switch Case Syntax
The syntax of JavaScript Switch Case is as follows:
Switch (expression) { Case Option 1: //Execute these statements when the expression result match Option 1 break; Case Option 2: //Execute these statements when the expression result match Option 2 break; Case Option 3: //Execute these statements when the expression result match Option 3 break; ...... Case Option N: //Execute these statements when the result of expression match Option N break; Default: //Execute these statements when the result of expression Not matching with any Option break; }
The expression should be either integer or characters (We can write the expression as n/2…. also, but the result should be an integer). Option 1, Option 2…… are constants.
Switch Case allows us to add a default statement. If the variable value is not matching with any of the case statements, then the code present in the default will be executed.
The Break statement is useful to come out from the switch statement. Otherwise, all the statements in the switch condition will execute. Whenever a break encountered, the execution flow would directly come out of the switch.
NOTE: Switch Case will only accept either integers or characters, whereas the Else if statement accepts decimal values (2.3, 3.5, etc.) also.
JavaScript Switch Case Flow Chart
Let us see the flow chart of the Switch case in JavaScript to understand the programming flow.
JavaScript Switch Example
This program allows us to use Arithmetic Operators to perform arithmetic operations using Switch Case.
Please refer to JavaScript Else Statement, JavaScript Nested If, and Break statement articles in JavaScript.
<!DOCTYPE html> <html> <head> <title> JavaScript Switch Case </title> </head> <h1> JavaScript Switch Case </h1> <body> <script> var opertor = '*'; var number1 = 10, number2 = 2; switch (opertor) { case '+': document.write("Addition of two numbers is: " + (number1 + number2)); break; case '-': document.write("Subtraction of two numbers is: " + (number1 - number2)); break; case '*': document.write("Multiplication of two numbers is: " + (number1 * number2)); break; case '/': document.write("Division of two numbers is: " + (number1 / number2)); break; case '%': document.write("Module of two numbers is: " + (number1 % number2)); break; default: document.write("<b> You have entered Wrong operator </b>"); document.write("<br \> Please enter Correct operator such as +, -, *, /, %"); } </script> </body> </html>
OUTPUT 1: Here we assigned * as operator variable.
OUTPUT 2: Let us change the operator from valid to the wrong operator to check the default value