0

I'm trying to get my head around the reason JS picks the last element (8):

var values = (null, 7, null, 8);
console.log(values); 
// Output: 8

console.log and alert behaves the same so I assume it's something to do with the language itself.

Deniz Ozger
  • 2,565
  • 23
  • 27

2 Answers2

5

From the first line of the docs:

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

Marty
  • 39,033
  • 19
  • 93
  • 162
0

This one also explained beautifully link

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