Loops in JavaScript are used to execute the same piece of code a specified number of times (or when a specified condition is true).
JavaScript Switch Statement
If you want to choose to execute one of several code blocks, you can use the switch statement:
Syntax:
switch(n)
{
case 1:
Execute code block 1
break
case 2:
Execute code block 2
break
default:
If n is neither 1 nor 2, execute this code
}
Working principle: behind switch (n) can be an expression or (and usually is) a variable. The value in the expression is then compared with the number in the case. If it matches a case, the following code will be executed. The function of break is to prevent the code from automatically executing to the next line.
Example:
]