You access an element of a JavaScript array with square brackets and a zero-based index, such as fruits[0] for the first item, and you read the number of elements from the length property. The last element is at index length - 1, or you can use at(-1). This guide explains how array indexes work, what happens with out-of-range positions, and how length behaves, including the surprising fact that you can write to it.
If you can read and write array[index] and use length confidently, you have the base for every other array method.
Array indexes start at 0. The first element is index 0, the second is index 1, and so on.
Use the same syntax to change an element:
Declaring the array with const does not stop you from changing its elements. It only prevents assigning a different array to the variable.
length returns the number of slots in the array, which is always one more than the highest index.
Because indexes start at 0, the last element is at length - 1:
Use length in loops so the code keeps working when the array changes:
For simple traversal, for (const fruit of fruits) is shorter and avoids index mistakes.
The at() method (ES2022) accepts negative numbers that count from the end.
Plain bracket syntax does not support negative indexes. fruits[-1] is undefined, because -1 is treated as an ordinary property name, not a position.
Reading an index that does not exist does not throw an error. It returns undefined.
That makes it hard to tell a missing element from an element that holds undefined. If you need to know whether an index exists, compare it with length or use the in operator:
Writing past the end is allowed and creates empty slots between:
Arrays with empty slots are called sparse. Avoid them, because many methods skip empty slots and some engines handle them more slowly. Use push() to add to the end instead.
length is writable. Setting it smaller removes elements permanently, and setting it larger adds empty slots.
The removed values are lost, so use this deliberately. Clearing an array with arr.length = 0 also changes every other variable that points to the same array, which is often what you want but can surprise you.
| Mistake | Problem | Fix |
|---|---|---|
arr[arr.length] to get the last item | Always undefined, one past the end | arr[arr.length - 1] or arr.at(-1) |
i <= arr.length in a loop | Runs one step too far | Use i < arr.length |
Using arr[-1] | undefined | Use arr.at(-1) |
Using arr.length as a count of items in a sparse array | Counts empty slots too | Avoid sparse arrays |
| Using an index after removing items | Points at a different element | Re-read the index or use find |
Also remember that length is not the size of memory. It is just the highest index plus one.
The index counts how far an element is from the start of the array, so the first element is 0 steps away. This convention comes from earlier languages such as C and is used by almost all mainstream languages.
Use arr.at(-1) in modern JavaScript, or arr[arr.length - 1] in any version. On an empty array both return undefined.
For normal arrays, yes. For sparse arrays, length counts empty slots too, because it is defined as the highest index plus one.
Index values are converted to property names, so arr["1"] works like arr[1]. Other strings such as arr["name"] set ordinary object properties that do not count towards length, and you should avoid them.
Read and write array elements with array[index], starting at 0. The length property is the highest index plus one, so the last item is arr[arr.length - 1] or arr.at(-1). Out-of-range reads give undefined, writing beyond the end creates a sparse array, and assigning to length truncates or extends the array.