This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by witwiki3
about 11 years

Logical Operators in Switch Statements

Hi,

I tried looking in the web for this but could not find it for front end related languages. But apparently in back end languages like php and I guess python, we can use logical, boolean and comparison operators in switch statements.

Is this true in javascript as well?

Can we use logical, boolean and comparison operators in the switch statements and the ‘case’ parts of the switch statements?

Thanks in advance.

Answer 50e70ba6e734b524700071e9

4 votes

Permalink

Yes indeed, we can use all three types of operators within switch statements. Here is a quick example I just made:

var number = prompt("Type in any number!");
switch(true) {
case (number < 50):
    console.log("case in which number is less than 50");
    break;
default:
    console.log("case in which number is not less than 50");
    break;
}

:-)

points
Submitted by Tom Neckermann
about 11 years

1 comments

Huji Nikolas over 10 years

Thank you so much that works fine, so the matter is when we need to use logical operator in case we must set the switch(true) not the switch (variable). Thanks a lot