What does a comma do in JavaScript expressions?

前端 未结 5 1562
礼貌的吻别
礼貌的吻别 2020-11-21 05:22

If I use:

1.09 * 1; // returns \"1.09\"

But if I use:

1,09 * 1; // returns \"9\"

I know that 1,09 isn\'t

5条回答
  •  星月不相逢
    2020-11-21 06:04

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

    Source: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/Comma_Operator

    For example, the expression 1,2,3,4,5 evaluates to 5. Obviously the comma operator is useful only for operations with side-effects.

    console.log(1,2,3,4,5);
    console.log((1,2,3,4,5));

提交回复
热议问题