Table of Contents
This guide covers switch case command in JavaScript with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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:
Syntax
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