Arrays
In short: an array is JavaScript's ordered list. Items are numbered from
zero, so a 3-item array runs from index 0 to index 2 — the last valid
index is always length - 1. Reading past the end does not throw an error;
it quietly gives undefined, which is where most array bugs come from.
Arrays are objects: a const array can still grow, and copying the name
shares the same array rather than duplicating it.

Read every position of a three-song playlist
Counting from zero means the first song sits at index 0, not 1:
playlist.length is 3, but the last real item is at index 2, not 3 —
length is a count, not an index. .at(-1) sidesteps the subtraction
entirely: negative positions count back from the end, so -1 is always the
last item, however long the array is. Reading playlist[3], one past the
end, gives undefined rather than an error.
The state
playlisthas 3 songs andcurrentis 2.
Loop over a playlist off by one
The off-by-one mistake shows up constantly in hand-written loops. Start the
index at 1 instead of 0, and stop at <= instead of <, and both ends go
wrong at once:
This skips "Blue Hour" entirely and ends by printing undefined — the
loop starts one index too late and finishes one index too far. The
corrected version starts at 0 and stops before length:
for (let i = 0; i < playlist.length; i++).
Grow a liked-songs array without losing const
Everyday array methods change the array's contents, which const
allows — it only blocks reassigning the name itself:
push adds to the end and returns the new length; pop removes from the
end and returns what it removed. includes asks "is this value in here?";
indexOf gives the position, or -1 when nothing matches.
The trap: assigning an array shares it
Because arrays are objects, pointing a second name at one does not copy it — both names see the same array:
This prints 3, not 2 — b never held a separate array, so pushing
through b is visible through a too. A real copy needs
const b = [...a];, which the Objects lesson's spread pattern also covers.
Quick practice
With const playlist = ["Blue Hour", "Red Dawn", "Green Tide"];, what is playlist[1]?
With the same playlist, what is playlist[playlist.length]?
What does const p = ["a", "b"]; p.push("c"); console.log(p.length); log?
With the three-song playlist, what is playlist.at(-1)?
What does const a = [1, 2]; const b = a; b.push(3); console.log(a.length); log?
🎯 Your turn
A loop for (let i = 1; i <= playlist.length; i++) { show(playlist[i]); }
skips the first song and shows undefined at the end. Explain both
problems and write the correct loop.
Write your explanation and submit it — there's no code to run here, just your reasoning.
