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

前端 未结 18 1612
感动是毒
感动是毒 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:57

    Hey mate just remember js works by evaluating to either true or false, right?

    let's take a ternary operator :

    questionAnswered ? "Awesome!" : "damn" ;
    

    First, js checks whether questionAnswered is true or false.

    if true ( ? ) you will get "Awesome!"

    else ( : ) you will get "damn";

    Hope this helps friend :)

提交回复
热议问题