1

I work on web site with pre-existent javascript code.

I found out that in this code, it used to retrieve an array item in this way:

var value = myArray[0, 1];

The result is the second field of the array, but I can't understand the difference with a code like:

var value = myArray[1];

I have tried to change the number before comma but nothing change, it reads always the second item of the array.

MERCCK
  • 63
  • 8
  • 3
    It's the JavaScript comma operator. A series of expressions separated by commas evaluates all of them, but the overall value of the series is the value of the *last* expression. Thus, in `0, 1` there are two expressions, and the last one is `1`. – Pointy Dec 17 '18 at 13:49

1 Answers1

2

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand. That's why you get 1.

devedv
  • 562
  • 2
  • 15
  • 45