</> Code Corner

JavaScript · JS Tutorial · Lesson 29

JS Switch

Select one of many code blocks.

The switch statement selects one of many code blocks to execute based on a value.

Example

<p id="demo"></p>
<script>
let day;
switch (new Date().getDay()) {
  case 0: day = "Sunday"; break;
  case 1: day = "Monday"; break;
  case 2: day = "Tuesday"; break;
  case 3: day = "Wednesday"; break;
  case 4: day = "Thursday"; break;
  case 5: day = "Friday"; break;
  case 6: day = "Saturday"; break;
  default: day = "Unknown";
}
document.getElementById("demo").innerHTML = "Today is " + day;
</script>
  • break stops the execution inside the switch block
  • default runs if there is no case match
  • Switch cases use strict comparison (===)

Keep reading “JS Switch”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋