How do you use the ? : (conditional) operator in JavaScript?

前端 未结 18 1639
感动是毒
感动是毒 2020-11-21 05:48

In simple words, what is the ?: (conditional, "ternary") operator and how can I use it?

18条回答
  •  忘了有多久
    2020-11-21 06:08

    z = (x == y ? 1 : 2);
    

    is equivalent to

    if (x == y)
        z = 1;
    else
        z = 2;
    

    except, of course, it's shorter.

提交回复
热议问题