What is [1,2,3,4] [1,2] in javascript

can someone explain this question with an explanation. I tried the console and the answer to question 3

[1,2,3,4][1,2] //consoles 3
+4
source share
3 answers

[1,2,3,4]is an array literal .

1,2- these are two numbers with a comma between them, therefore it is allowed 2.

So you get index 2 (third element) from the array.

var array = [1,2,3,4];
var property = (1,2);
var result = array[property];

console.log({ array: array, property: property, result: result });
Run code
+6
source

This is a immediately invoked semicolon array.

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

for the index.

[1, 2, 3, 4][1, 2]

permits

[1, 2, 3, 4][2] // 3
+7
source

[1,2,3,4] 4 .

The second [1,2]is parenthesis notation (used here to access an element of the aforementioned array).

Inside this bracket is the comma operator that evaluates its rightmost largest expression 2.

So:

[1,2,3,4][1,2]

matches with:

[1,2,3,4][2]

which matches with:

var arr = [1,2,3,4];
arr[2];
+6
source

Source: https://habr.com/ru/post/1675800/


All Articles