Javascript - Ternary Operator with Multiple Statements

后端 未结 5 1913
Happy的楠姐
Happy的楠姐 2020-11-29 22:29

Is this valid JavaScript? I saw an example where someone used commas in the ternary operator conditions, and it was marked as an error in my editor, and the example didn\'t

5条回答
  •  半阙折子戏
    2020-11-29 22:53

    Or you can do this :

    b = a!==b ? (a=1,2) : (a=2,1);
    

    Read here about comma operator.

    The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

提交回复
热议问题