(Sorry for my bad English)
Hi everyone!
I'm new to JS!
I'm learning about Switch currently and a little bit confused about it.
More specifically, when should I use "true" in the switch and when to use the original variable name!
For example, this is my code!
var age = 10;
switch(true){
case age>=10:
console.log('He is a kid');
break;
case age>10 && age<=20:
console.log('He is a teenager');
break;
case age>=20 && age<=30:
console.log('He is a young man');
break;
case age>30:
console.log('He is a man')
}
This works totally fine if I do switch(true) but if I do what most people say switch(age), this doesnt work!
So i'm a bit confused when to use what!
My guessing, if I'm doing greater than, equal to and less than type of things with numbers, then you you true in the swithc!
Can anyone please help?