Here is a simple switch case scenario:
let ca: string = "2";
switch (ca) {
case "2":
console.log("2");
case "1":
console.log("1");
default:
console.log("default");
}
I am puzzled by the output of this code, which is as follows:
2
1
default
My anticipated output would be
2
default
Why does it print
1
even though ca does not equal "1"?
EDIT: I am aware that adding a break
statement could stop this behavior - however, I am trying to understand why case "1"
is executed when ca="2"
Thank you.