InterviewHubPRO
Section Overview
Q429

Question 429 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

82%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

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,

js
 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);