Javascript Ternary operator with empty else

后端 未结 7 744
梦如初夏
梦如初夏 2020-12-18 13:43

I\'m trying to convert the following if-else to it\'s ternary operator representation in javascript as follows

var x = 2;
if (x === 2) {alert(\"2\");}
else
          


        
7条回答
  •  梦毁少年i
    2020-12-18 13:58

    NO, you can't have empty else, better don't use the ternary operator, it requires a third argument. Go with simple if condition.

    if(t==2) alert("2");
    

提交回复
热议问题