Skip to content

Instantly share code, notes, and snippets.

@schabibi1
Created February 12, 2019 20:21
Show Gist options
  • Save schabibi1/6088c5900e32460ad74d858ae340b81c to your computer and use it in GitHub Desktop.
Save schabibi1/6088c5900e32460ad74d858ae340b81c to your computer and use it in GitHub Desktop.
JavaScript: Is Fallthrough From Switch Statement A Troublemaker?
<html>
<body>
<h1>Message changes depends on the day you got.</h1>
<button onclick="changeDay()">Click Me</button>
<h3 id="message"></h3>
<script>
function changeDay() {
let text = '';
switch (new Date().getDay()) {
case 1:
case 2:
case 3:
default: // default is NOT the last case in the switch block & no break
text = "Give my Weekend back 😩";
// supposed to break in here
case 4:
case 5:
text = "Almost Weekend...!! 😎"; // You get this message although your case is 1/2/3
break;
case 0:
case 6:
text = "Weekend!!!! 🤪";
}
const message = document.getElementById("message");
message.innerHTML = text;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment