javascript switch(true)

前端 未结 4 1681
别那么骄傲
别那么骄傲 2020-12-01 18:19

Hi i am trying to handle an ajax json response

here is my code

success: function (j) {     
    switch(true)
    {
        case (j.choice1):

                


        
4条回答
  •  没有蜡笔的小新
    2020-12-01 18:28

    It works for me:

    var a = 0, b = true;
        
    switch(true) {
        case a:
            console.log('a');
            break;
        case b:
            console.log('b');
            break;
    }

    However, the case labels must be equal to true, not jut implicitly true.
    Also, only the first case that evaluates to true will execute.

提交回复
热议问题