Why does this switch statement run twice?

  javascript, switch-statement

I have this switch statement and when I enter sunny it outputs:

Dress lightly
Go Outside

Why is that? I thought omitting the break statement would only let the program check all other options, but it looks like all cases are valid until a break statement is apparent.

Can you please explain the behavior of switch in terms of breaking out of its body?

Code

switch (prompt("What is the weather like today?")) {
    case "rainy":
        console.log("Never forget your umbrella mate!");
        break;
    case "sunny":
        console.log("Dress lightly");
    case "cloudy":
        console.log("Go outside");
        break;
    default:
        console.log("Unknow weather type");
        break;
}

Source: Ask Javascript Questions

LEAVE A COMMENT