A Javascript switch statement is used to execute a block of statements based on the switch expression value. It is like an if else if statement.
Syntax:
switch(expression){ case value1: //Javascript block of statements break; case value2: //Javascript block of statements break; ... default: //Javascript block of statements break; }
JavaScript Switch Statement Example:
<html> <head> <script> var today; switch (new Date().getDay()) { case 0: today = "Sunday"; break; case 1: today = "Monday"; break; case 2: today = "Tuesday"; break; case 3: today = "Wednesday"; break; case 4: today = "Thursday"; break; case 5: today = "Friday"; break; case 6: today = "Saturday"; } document.write("Today is " + today); </script> </head> <body> </body> </html>