-1
var arr1 = [1,2,3,4]; 
var arr2 = [1,2];

console.log(arr1+arr2); output is "1,2,3,41,2"

console.log(arr1,arr2); output is Array [1, 2, 3, 4] Array [1, 2]

The output 3 in console.log([1,2,3,4][1,2]); What is operation/type in JavaScript?

sameera lakshitha
  • 1,925
  • 4
  • 21
  • 29

1 Answers1

2

In the expression:

console.log([1,2,3,4][1,2]);

the comma in the second set of square brackets is acting as the comma operator.

As seen in the MDN docs, the comma operator evaluates each side of the comma and returns the final value.

In this case, we get:

console.log([1,2,3,4][2]);

Which returns the third value, that being the one at array subscript 2.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130