Search code examples
javascriptmathoperations

Why the result is 8?


Today I had a job interview on JS and one of the questions was

What is the value of a

var a = (3,5 - 1) * 2;

Hmm I assumed its 8 (the chrome dev console also gives it 8) but I don't know why it is eight. Why the 3 is omitted? I mean of course you can't do any operations with it but still, it disappears, or I am wrong?

Please go easy on me. I am trying to understand. Any articles on this kind of operations would be highly appreciated


Solution

  • As per the Comma Operator | MDN,

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

    So, var a = (3,5 - 1) * 2; returns 4 * 2 = 8