Arrays
One Variable, Many Values
Every variable you have created so far holds exactly one value. But real programs deal with collections: a list of students, a series of temperatures, a set of prices.
You could create separate variables:
const student1 = "Rahul";
const student2 = "Priya";
const student3 = "Arjun";
But what happens when you have 300 students? Or when the number changes at runtime? You need a container that holds many values under one name, in order. That is an array.
Creating Arrays
An array is written with square brackets [], values separated by commas. It can hold any types — numbers, strings, booleans, even other arrays. The .length property tells you how many elements it contains.
Accessing Elements
Arrays are zero-indexed — the first element is at position 0.
planets.at(-1) is modern JavaScript — negative indices count from the end. -1 is the last, -2 is second to last, and so on.
Modifying Arrays
Arrays are mutable — unlike strings, their contents can be changed directly.
The four mutation methods you will use constantly:
push(value)— add to endpop()— remove from end, returns removed valueunshift(value)— add to beginningshift()— remove from beginning, returns removed value
Slicing and Splicing
slice is non-destructive — it returns a new array, the original is untouched. splice is destructive — it modifies the array in place. Know which one you need before you reach for it.
Searching
The Big Three: map, filter, reduce
These three methods are the most powerful tools arrays give you. Together they can replace almost every loop you would otherwise write. They each take a function as an argument — which is why Lesson 5 had to come first.
map — Transform Every Element
map creates a new array by applying a function to every element of the original.
The original array is never modified. map always returns a brand new array of the same length.
filter — Keep Only What Passes
filter creates a new array containing only the elements for which the function returns true.
Notice how filter and map are chained — the result of filter is an array, so you call .map() on it directly. This style — chaining array methods — is elegant, readable, and very common in modern JavaScript.
reduce — Collapse to a Single Value
reduce is the most powerful and the most misunderstood. It processes every element and accumulates a single result — a sum, a product, an object, another array, anything.
The function passed to reduce takes two arguments:
accumulator— the running result so farcurrent— the current element being processed
The second argument to reduce itself (after the comma) is the initial value of the accumulator.
Read the sum version as: "Start with 0. For each price, add it to the running total. Return the final total."
Sorting
This is a famous JavaScript trap. By default, .sort() converts elements to strings and sorts lexicographically — so [10, 1, 21] becomes [1, 10, 21]... except 100 sorts before 21 because "1" < "2". Always pass a comparator function when sorting numbers.
The comparator rule: if (a, b) => a - b returns negative, a comes first. If positive, b comes first. If zero, order doesn't matter.
Flattening and Spreading
The spread operator ... is one of the most useful tools in modern JavaScript. It unpacks an array's elements. When you write [...a, ...b], you are creating a new array containing all elements of a followed by all elements of b.
Copying with [...a] is important: writing const copy = a does not copy the array — it creates a second label pointing to the same array. Changes through copy would affect a. The spread copy creates a genuinely separate array.
A Complete Pipeline
This is a data pipeline — the kind of code you write every day in real applications. Filter, sort, transform, aggregate. No loops visible, just a clean chain of intentions.
What You Have Learned
Arrays are the most-used data structure in JavaScript. The tools:
- Create with
[], access with[index], length with.length - Mutate with
push,pop,shift,unshift,splice - Extract with
slice(non-destructive) - Search with
indexOf,includes,find,findIndex map— transform every element → new array same lengthfilter— keep elements that pass a test → new array shorter or equalreduce— collapse all elements → single value of any type- Sort with a comparator function for numbers — never rely on default sort
- Copy safely with the spread operator
[...array]
In the next lesson: Objects — how to group related values under named properties, and how objects and arrays work together to represent any real-world data structure.