JS Basic Tutorial
The switch
statement is used to execute one block of code among many options. It evaluates an expression, matches the result to a case
, and executes the associated block.
switch (expression) { case value1: // Code to execute if expression === value1 break; case value2: // Code to execute if expression === value2 break; // Add more cases as needed default: // Code to execute if no case matches }
Try it yourself