What an array is
0.[7, 3, 9, 4, 12, 6]. All day the app asks "what did I walk on day 4?" — thousands of times a second. How you store those numbers, and how fast you can reach one, is the whole ballgame.- day 1's steps sit at index
0 - day 2's at index
1 - the last day at index
length - 1(for six days, index5)
i lives and jump straight to it — it never walks past the earlier values, no matter how long the row grows. So "day 4's steps" is just steps[3], answered instantly.This section has a step-by-step animation in the interactive lesson.
Quick check
An array holds 8 values. Its last valid index is…
Answer it in the interactive lesson to keep your progress.
The core operations
steps = [7, 3, 9, 4, 12, 6]:- access
steps[3]— jump straight to slot 3 →4. Instant, never touches slots 0–2. - update
steps[2] = 10— overwrite a slot in place. - push / pop — add or remove at the end. Only the last slot moves.
- unshift / shift — add or remove at the front. Every other value has to slide over to make room.
- search
steps.indexOf(12)— you don't know the slot, so you scan until you find it.
O(1)), and so is adding at the end. But anything at the front, or finding a value you can't name by number, means touching many slots (O(n)).The Core Operations in JavaScript
const steps = [7, 3, 9, 4, 12, 6];
console.log(steps[3]); // 4 (access by index — instant)
steps[2] = 10; // update in place
steps.push(8); // add at the end
steps.pop(); // remove from the end
console.log(steps.indexOf(10)); // 2 (search by value — a scan)Quick check
Before moving on, predict: which of these is the expensive one?
Answer it in the interactive lesson to keep your progress.
Your first array problem: find a value
9 in [7, 3, 9, 4, 12, 6]. We can't jump straight to it, because we don't know its index. So the plan is the simplest thing that always works: look at each slot in turn until we land on it. That's called a linear search — linear because, in the worst case, we walk the whole line once.The steps
- Start at the first slot, index
i = 0. - Does slot
ihold the value we want? If yes, the answer isi— stop. - If not, step one slot right (
i = i + 1) and go back to step 2. - If we run off the end, the value isn't here.
Trace it, looking for 9:
- slot
0→7? no - slot
1→3? no - slot
2→9? yes — return index2
n slots — so linear search is O(n), linear time. Simple, always works, and it's the baseline every fancier array trick tries to beat.i pointer hopping slot to slot.)Linear Search in JavaScript
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) { // walk each slot
if (arr[i] === target) return i; // found it → its index
}
return -1; // ran off the end → not here
}
console.log(linearSearch([7, 3, 9, 4, 12, 6], 9)); // 2Quick check
Why might linear search have to check every element?
Answer it in the interactive lesson to keep your progress.
Where it breaks (and how not to get burned)
- Off by one. The last valid index is
length - 1, notlength. Loop withi < arr.length; readingarr[arr.length]gives youundefined, not the last value. - The hidden
O(n²). A linear search isO(n). Do one inside a loop over the same array — anindexOfper element — and you've quietly built a quadratic (O(n²), from the Complexity lesson: a loop inside a loop). It crawls on big inputs. Sort once and binary-search, or keep a hash map. - Front-inserting in a loop. Every
unshiftslides the whole array over (O(n)each time). Build backwards and reverse at the end instead.
Quick check
Searching an unsorted array inside a loop over the same array costs…
Answer it in the interactive lesson to keep your progress.
Take it further — prefix sums
prefix[i] holds the running total of the first i values. After that, the sum of any range is a single subtraction — prefix[j+1] - prefix[i] — instead of re-adding. One O(n) setup buys you O(1) answers per query. (This idea gets a full lesson of its own — Prefix Sums, two stops from here.) Total steps for days 1–3 of [7, 3, 9, 4, 12, 6]:prefix cell is just previous total + next value — one addition per step. Then the query: the l and r+1 pointers mark the range, and a single subtraction answers it without touching the values in between.Building a Prefix Sum in JavaScript
const days = [7, 3, 9, 4, 12, 6];
const prefix = [0];
for (const x of days) prefix.push(prefix[prefix.length - 1] + x);
// days 1..3 (7 + 3 + 9 + 4) = prefix[4] - prefix[0]
console.log(prefix[4] - prefix[0]); // 23Quick check
With prefix = [0, 7, 10, 19, 23, 35, 41], predict: the sum of indices 1..2 is…
Answer it in the interactive lesson to keep your progress.
How fast is it? (complexity)
| Operation | Cost | Why |
|---|---|---|
| access / update by index | O(1) | jump straight to the slot |
| push / pop at the end | O(1) | only the last slot moves |
| unshift / shift at the front | O(n) | everything slides over |
| search an unsorted array | O(n) | the value could be anywhere |
n values takes O(n) space.- An array is a row of numbered slots; indices run
0 … length - 1. - Access by index is free (
O(1)); blind search is a walk (O(n)). - End operations are cheap; front operations shift everything.
- Learn to smell a naive scan hiding an
O(n²)before it bites.
What's next
O(n) or O(n²) scan, and each gets its own lesson later — no need to know them yet:- Two Sum — find a pair that adds to a target in one pass, using a hash map.
- Maximum subarray (Kadane's) — the best run of consecutive days, in a single sweep.
- Sliding window & two pointers — the longest or shortest stretch that satisfies a rule.