I dont understand why my function doesnt take the value 70.
let test = (age) => {
switch (age) {
case (age > 65):
console.log("High age")
break
default:
console.log("Low age")
break
}
}
test(70)
I dont understand why my function doesnt take the value 70.
let test = (age) => {
switch (age) {
case (age > 65):
console.log("High age")
break
default:
console.log("Low age")
break
}
}
test(70)
2
Answers
If there’re only two options then simply use
if-else
statement:BTW The problem with your code is that boolean expressions like (age > 65) are evaluated to be either true or false, neither of which is equal to age itself.
To use a switch statement, you could modify your code like this: