JavaScript Arrays explanation please?

Hey all,

I have just completed the arrays section of JavaScript but I was a little confused on this section:

This is the solution:

const numberClusters = [[1, 2], [3, 4], [5, 6]];

const target = numberClusters[2][1];

The thing I don’t get is why is it [2][1]?

Arrays are 0-indexed.
That means the first item is index 0, the second item index 1, and so forth.

So let’s say letterObjects = [a, b, c] and you want to access c…

  • you would call the index-2: [2].

  • Now, inside c, there’s another list [a, b], and you want to access b, that would be the index-1.

  • If you wanted to call the item b that belongs to c, you would call for [2][1], which can read as:

from index-2 give its index-1, or by my example:
from c, give me b.

1 Like

Adding on to toastedpitabread’s answer, the concept of [2][1], for example in;

const someArray = [[1, 2], [3, 4], [5, 6]];

is known as chaining. Similar to how you chain the .log() method to the console object, chaining index values simply means that the things being chained are connected, as the word ‘chain’ implies. Thus [2][1] in this context , means look inside the element with index value 2 in the array, and find the element inside that nested array that has index value 1.

1 Like