(function() {
var myVarOne = [10, 20, 30, 40][1, 2, 3];
var myVarTwo = [10, 20, 30, 40][3, 2, 1];
console.log("myVarOne = "+myVarOne);
console.log("myVarTwo = "+myVarTwo);
})();

myVarOne = 40
myVarTwo = 20
-
At first look it may seem like two arrays in conjunction. But the second brackets are evaluated using comma operator and not considered like an array. The first one is indeed an array.
-
When we look at
myVarOne
, the last value[1, 2, 3]
is considered or evaluated as 3. Hence like an arraymyVar[1, 2, 3]
as if it were equivalent tomyVar[3]
. However, this is not entirely true. The parsing happens as follows:var myVarOne = [10, 20, 30, 40][((1, 2), 3)]; var myVarTwo = [10, 20, 30, 40][((3, 2), 1)];
-
When [10,20,30,40][1,2,3] is parsed, [ is understood as the start of the array or object property accessor. Hence the expression inside the accessor is simply treated as a vanilla expression with the comma operator. The confusion mainly lies in that the reader expects [1,2,3] to be an array literal as well.
-
The comma operator can be mostly seen as used in the multiple variable declaration a.k.a. single var pattern.
- MDN on comma operator
-
[Single var pattern](http://sixrevisions.com/javascript/single-var-pattern)