What is “?:” notation in JavaScript?

后端 未结 4 2031
北恋
北恋 2020-11-30 10:19

I found this snippet of code in my travels in researching JSON:

var array = typeof objArray != \'object\' ? JSON.parse(objArray) : objArray;
<
4条回答
  •  爱一瞬间的悲伤
    2020-11-30 10:33

    It's the ternary conditional operator -- basically,

    if (condition) {
       a = 4;
    }
    else {
       a = 5;
    }
    

    becomes

    a = condition ? 4 : 5;
    

提交回复
热议问题