0

Following is my javascript code:-

var time = 21
var text;
switch (time) {
  case 21:
    text = "1st case";
  case 22:
    text = "2nd case";
  case 23:
    text = "3rd case";
  default:
    text = "default";
}
console.log(text);

In the above code I wanted to know in line 9 and 11, why does this default statement also run and changes the text variable to "default"?

I wanted a little bit detailed explanation for this, according to my knowledge and a bit of gathering information I found that this will follow the rule of fall through, but I wanted to know why... or maybe there is another breakdown to this code.

NOTE - Removing break statement was intentional.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875

0 Answers0