FrontendDeveloper.in

JavaScript question detail

Is that possible to use expressions in switch cases?

You might have seen expressions used in switch condition but it is also possible to use for switch cases by assigning true value for the switch condition. Let's see the weather condition based on temperature as an example,

const weather = (function getWeather(temp) {
switch (true) {
case temp < 0:
return "freezing";
case temp < 10:
return "cold";
case temp < 24:
return "cool";
default:
return "unknown";
}
})(10);
Back to all JavaScript questions
Get LinkedIn Premium at Rs 399