Hello friends, "True", what is inside the parentheses of the "switch" command? Thank you for your help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hello friends, "True", what is inside the parentheses of the "switch" command? Thank you for your help

var age=parseInt(window.prompt("Please enter your age")); switch(true){ case (age<12): window.alert("You are a child"); break case (age>=12 && age<=15): window.alert("You are a teenager"); break case (age>15 && age<=35): window.alert("You are young"); break case (age>35 && age<=60): window.alert("You are middle-aged"); break; case (age>60 && age<=100): window.alert("You are an old man"); break case (age>100): window.alert("You are not alive"); break default: window.alert("You entered the wrong age"); break; }

2nd Jul 2021, 4:55 AM
Hadital
Hadital - avatar
3 Answers
+ 1
Would you like to know why they used true as switch expression?
2nd Jul 2021, 5:18 AM
Simba
Simba - avatar
+ 2
It's a method to use logical operations and conditions in switch statement. Switch has to be true for prompt to work. For example, age=50 is true for case (age<35 && age<=60). So, output will be "You are middle-aged"
2nd Jul 2021, 11:41 AM
Simba
Simba - avatar
0
Yes bro
2nd Jul 2021, 10:28 AM
Hadital
Hadital - avatar