Array Fundamentals
Intermediate · 18 min read · ▶ live playground · ✦ checkpoint
Arrays are JavaScript's everyday way to keep ordered data together: names, scores, todos, rows from a file, search results from an API. The beginner trap is that arrays look like simple lists, but they are objects underneath — so a const array can still change, and two names can point at the same array.
Arrays hold values in order
Create an array with square brackets. Each value inside is an element, and each element has an index — its position number.
const scores = [10, 14, 8];
console.log(scores.length);
console.log(scores[0]);
console.log(scores[2]);
scores[1] = 20;
scores[scores.length] = 17;
console.log(scores.length);
console.log(scores[1]);
console.log(scores[3]);→ 3, 10, 8, then 4, 20, 17.
Indexes are zero-based: the first item is at 0, the second is at 1, and the last regular index is array.length - 1. You have already seen that pattern with strings; arrays use the same bracket-reading shape.
Writing to an existing index replaces that element. Writing to scores[scores.length] adds one element at the next open slot, because length is one more than the last regular index.
Prefer array literals ([]) over older new Array(...) examples you may see online. The literal is shorter, clearer, and avoids a few constructor surprises that are not worth importing into modern code.
length is not a quality check
length tells you how far the array stretches. It does not prove every slot contains a useful value.
const checklist = ["plan"];
checklist[3] = "ship";
console.log(checklist.length);
console.log(checklist[1]);
console.log(checklist[2]);
console.log(checklist[3]);→ 4, then undefined, undefined, ship.
That array is sparse: it has empty slots at indexes 1 and 2. Reading those slots gives you undefined, which looks like a normal missing value from the outside. The important habit is simpler than the engine detail: do not jump indexes on purpose. Add items at the end, or use the array methods coming in 6.2.
length is also writable. Shrinking it cuts items off the end; growing it creates more empty slots. Professional JavaScript almost always changes array size through methods like push and pop, or creates a shorter copy with slice, not by manually assigning length.
A const array can still change
const protects the variable binding — the name. It does not freeze the object the name points at.
const players = ["Mina", "Rae"];
const finalists = players;
finalists[0] = "Kai";
console.log(players[0]);
console.log(finalists[0]);→ Kai, then Kai — both names refer to the same array.
The move JavaScript refuses is reassigning the const name to a different array:
const players = ["Mina", "Rae"];
players = ["Kai"];That produces TypeError: Assignment to constant variable. Mutating the array through an index is allowed; moving the players name to a different array is not.
An alias is another name for the same array. A copy is a different array that starts with the same element values. Until 6.2 gives you copying methods, you can see the difference with a plain loop:
Try it — alias or copy?
Run this, then edit the two assignment lines. At the › prompt, type original, alias, and copy to inspect the arrays directly.
alias changes original because they are two names for one array. copy does not change original because the loop built a second array and placed the values into it.
That sentence hides a lot of future power. Most real programs keep arrays in const variables, then create new arrays when they want a safe copy. You will get the comfortable tools for that in the next two array lessons.
Nested arrays
An array can contain any kind of value, including another array. That gives you nested arrays, often used for grids, tables, and boards.
const board = [
["Mina", "Rae"],
["Sol", "Kai"],
];
console.log(board[0][1]);
board[1][0] = "Noor";
console.log(board[1][0]);
console.log(board.length);
console.log(board[0].length);→ Rae, then Noor, 2, 2.
Read board[0][1] left to right: get row 0, then get index 1 inside that row. There is no special "multidimensional array" type here — just arrays inside arrays. If one row is shorter than another, JavaScript allows it, so your code must know the shape it expects.
Arrays and array-likes
Arrays belong to JavaScript's object family, so typeof gives the not-very-helpful answer "object". Use Array.isArray(...) when you need to know whether a value is a real array.
const names = ["Mina", "Rae"];
const word = "Mina";
console.log(Array.isArray(names));
console.log(Array.isArray(word));
console.log(word.length);
console.log(word[0]);
console.log(typeof names);→ true, false, 4, M, object.
word is array-like: it has a length and bracket access, but it is not an array. Strings are still immutable primitives, so word[0] = "K" will not turn "Mina" into "Kina". Later, in the browser and in some libraries, you will meet more array-like values. The test stays the same: if you need a real array, ask Array.isArray(value).
Checkpoint
Answer all three to mark this lesson complete
You now have the mechanics: create arrays, read and write indexes, watch length, avoid sparse holes, and recognize when two names share one array. Next, 6.2 gives you the essential array methods so you can add, remove, copy, and search without hand-writing every index move.