is switch(true) {… valid javascript?

后端 未结 5 1797
暗喜
暗喜 2020-12-31 00:59

I recently came across code where a switch statement seemed reversed with the answer (boolean) in the switch and the expressions in the case. The code ran fine as intended b

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 01:21

    The cases will get executed based on value of y.

    Because the conditions depend on the value of y. As said by aefxx, it is an other form of:

    if (y < 20) {
        // ...
    } elseif (y < 60) {
        // ...
    } elseif ( y < 130) {
        // ...
    }
    

提交回复
热议问题