switch statement to compare values greater or less than a number

前端 未结 3 1921
夕颜
夕颜 2020-12-29 00:10

I want to use the switch statement in some simple code i\'m writing.

I\'m trying to compare the variable in the parenthesis with values either &l

3条回答
  •  爱一瞬间的悲伤
    2020-12-29 01:08

    Directly it's not possible but indirectly you can do this

    Try like this

    switch (true) {
        case (age < 13):
            alert("You must be 13 or older to play");
            break;
        case (age >= 13):
            alert("You are old enough to play");
            break;
    }
    

    Here switch will always try to find true value. the case which will return first true it'll switch to that.

    Suppose if age is less then 13 that's means that case will have true then it'll switch to that case.

提交回复
热议问题