I’m practicing using switch statements and wondered whether it is possible to use conditional operators? For example, with an if else statement I could write the following:
let a = 5;
if (a < 5) {
console.log('Less than 5');
} else if (a === 5) {
console.log('5');
} else if (a < 10) {
console.log('Less than 10');
} else if (a === 10) {
console.log('10');
} else {
console.log('Greater than 10!');
}
However, when I tried to use a switch statement such as the one below it doesn’t work as I would expect… the code below just prints ‘Greater than 10!’
let a = 5;
switch (a) {
case a < 5:
console.log('Less than 5');
break;
case a === 5:
console.log('5');
break;
case a < 10:
console.log('Less than 10');
break;
case a === 10:
console.log('10');
break;
default:
console.log('Greater than 10!');
}