JS engineindex 0index 1index 2index 3lengthConsoleapple:23:3plum:6apple:7kiwi:7fig:7plum:71const fruits = ["apple", "pear", "fig"];2console.log(fruits[0]);3console.log(fruits.length);4fruits[1] = "kiwi";5fruits.push("plum");6console.log(fruits[fruits.length - 1]);7for (const f of fruits) console.log(f);applepearfigapple3kiwiplum3plumapplekiwifigplum
Back: the JavaScript engine, the array's numbered slots, its length, and the consoleFront: the script fruits.jsTokens: instructions, and the values moving in and out of the slotsAt the back, the JavaScript engine wired to one array, drawn as numbered slots (index 0, 1, 2, and a fourth that push makes), with its length beside it, and the developer console. In front, the script fruits.js. Line 1 fills slots 0 to 2; lines 2 and 3 read slot 0 and the length; line 4 replaces slot 1; line 5 grows the array; line 6 reads the last slot; line 7 visits every slot in order. The slots side by side are a picture of the idea. Engines store arrays in different ways inside, but indexes, length and push always behave as shown.

What an array is

  1. An array is one value that holds a list of values, in order. Each item sits in a numbered slot. That number is its index, and counting starts at 0, not 1.

Reading items

Changing and growing

Visiting every item

What happens

// step 1/8 · array
array    one value holding a list, in order
item     one value in the list
index    the number of its slot: 0, 1, 2, ...
         the first item is at index 0
length   how many items there are

In JavaScript an array is a kind of object: typeof ["a"] is "object", and Array.isArray() tells arrays apart. fruits holds a reference to the array, which is why const fruits can still have its items changed. The slots side by side are a picture of the idea. Engines store arrays in different ways inside, but indexes, length and push always behave as shown.