JavaScript: arrays
An array stores an ordered collection of values. JavaScript uses zero-based indexes, so the first item is at index 0.
What you will learn
- How to create and read an array
- How to update an item and inspect its length
- How indexes and missing items behave
Minimal example
const colors = ['red', 'green', 'blue'];
console.log(colors[0]); // "red"
colors[1] = 'lime';
console.log(colors.length); // 3Use square brackets to read or update an item. The length property tells you how many positions the array currently contains.
Common mistakes
- Do not use index
1when you mean the first item; indexes start at zero. - An out-of-range index returns
undefined; check that an item exists before using it. - Use array methods such as
map,filter, andfindwhen they express the operation clearly.