Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Structures  ›  Lesson

Arrays

Arrays 12 minAn array is an ordered list whose items are numbered from zero, so the last item is at length minus one.

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.

Four slots: "Blue Hour" index 0, "Red Dawn" index 1, "Green Tide" index 2, then a dashed "index 3 / undefined" slot — with playlist.length = 3, last valid index = length - 1 = 2, and playlist.at(-1) arcing back to "Green Tide" from the end

Read every position of a three-song playlist

Counting from zero means the first song sits at index 0, not 1:

javascript

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.

Getting the next songStep 1 of 4
  1. The state

    playlist has 3 songs and current is 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:

javascript

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:

javascript

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:

javascript

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

quick drill

With const playlist = ["Blue Hour", "Red Dawn", "Green Tide"];, what is playlist[1]?

quick drill

With the same playlist, what is playlist[playlist.length]?

quick drill

What does const p = ["a", "b"]; p.push("c"); console.log(p.length); log?

quick drill

With the three-song playlist, what is playlist.at(-1)?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe missing last song

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.