Home >> Javascript >> Switch statement in Javascript

Switch statement in Javascript


The switch statement is used to select one block of many code blocks to be executed.

Syntax

switch(expression){
   case x:
     //code block
     break;
   case y:
     //code block
     break;
  default:
     //code block
}

Example

var x = 1;
switch (x) {
  case 1:
    text = "One";
    break;
  case 2:
    text = "Two";
    break;
  default:
    text = "No value found";
}

Post Your Comment

Next Questions
loop
For loop
While loop
Do while loop
Functions
Objects
Array
Events
Onchange event
Onclick event
Onmouseover event
Onmouseout
Onkeydown event
Onload event

Copyright ©2022 coderraj.com. All Rights Reserved.