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

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

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

18条回答
  •  南笙
    南笙 (楼主)
    2020-11-21 05:53

    x = 9
    y = 8
    

    unary

    ++x
    --x
    

    Binary

    z = x + y
    

    Ternary

    2>3 ? true : false;
    2<3 ? true : false;
    2<3 ? "2 is lesser than 3" : "2 is greater than 3";
    

提交回复
热议问题