Aryan PrajapatKnowledge Contributor
Is that possible to use expressions in switch cases?
Is that possible to use expressions in switch cases?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
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);