Quiz Entry - updated: 2026.07.05
How do you create and access arrays in JavaScript?
An array is an ordered list of values addressed by a numeric index that starts at 0.
An array is JavaScript's way of holding a sequence of values in one variable. Conceptually it's a set of key-value pairs where the keys are whole numbers starting at 0 — so the first element lives at index 0, the second at 1, and so on. You create one with square brackets:
let animals = ['lion', 'rat']; // index 0 is 'lion', index 1 is 'rat'
console.log(animals[0]); // 'lion'
console.log(animals[1]); // 'rat'
animals[1] = 'fish'; // replace 'rat' with 'fish'
animals[2] = 'crocodile'; // add a new element at index 2
Key points and the classic gotcha:
- Indices start at 0, not 1 — so a 3-element array has indices
0,1,2. Off-by-one mistakes here are extremely common. - Arrays can hold mixed types:
[1, 'hello', true]is perfectly valid. - Arrays are dynamic — they grow and shrink as you add or remove elements; you don't fix a size up front.
Go deeper:
MDN: Indexed collections — creating and indexing arrays, the
lengthproperty, and the full set of built-in array methods.