Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undestanding double bracket operation in Javascript

Tags:

javascript

What does [1,2,3][1,2] means in Javascript? I do not understand what its supposed to do, and i have no clue how could i google such thing.

Any ideas?

I assume this is quite the newbie question, please forgive my ignorance.

like image 781
Ricardo Jacas Avatar asked Dec 17 '13 21:12

Ricardo Jacas


1 Answers

  • [1,2,3] is an Array literal
  • <obj>[p] is the bracket notation for property access
  • 1, 2 is a comma operator expression that evaluates to 2

So the [1,2,3][1,2] as a whole accesses the index 2 of the array, and yields 3.

like image 59
Bergi Avatar answered Sep 30 '22 17:09

Bergi