JS Coding Questions Logo
JS Coding Questions
#425💼 Interview💻 Code

Is that possible to use expressions in switch cases?

Advertisement

728x90

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
1const weather = (function getWeather(temp) {
2
3switch (true) {
4  case temp < 0:
5  return "freezing";
6  case temp < 10:
7  return "cold";
8  case temp < 24:
9  return "cool";
10  default:
11  return "unknown";
12}
13})(10);

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 82

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
425of476