Arrays: Create, Mutate & Access
Master the fundamental data structure of the web. Learn how to store ordered lists, access data, and distinguish between mutating and non-mutating methods.
The Array Data Structure
An Array is an ordered list of values. Unlike Objects, which use string keys to identify data, Arrays use numbered indexes.
Arrays are created using square brackets [], and their indexes always start counting at zero (0).
Mutating Methods (Destructive)
JavaScript provides several built-in methods that permanently change (mutate) the original array.
push(val) adds to the end, while pop() removes from the end.
unshift(val) adds to the beginning, while shift() removes from the beginning.
splice(start, deleteCount, ...items) is the ultimate mutator, capable of adding, removing, or replacing items anywhere in the array.
Non-Mutating Methods (Safe)
Some methods deliberately avoid changing the original array. Instead, they calculate a result and return a brand **new array**.
The most common is slice(start, end). It extracts a section of an array and returns it as a new copy, leaving the original perfectly intact.
concat(arr) merges two arrays together, returning a new, combined array.
The Array Workshop
Experiment with mutating methods (splice) versus non-mutating methods (slice) to see exactly how they impact the original data.
The Array Workshop
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- Arrays are ordered collections accessed via zero-based numeric indexes.
push(),pop(),shift(),unshift(), andsplice()permanently mutate the array.slice()andconcat()are non-mutating; they return a brand new copy.- The
.lengthproperty automatically updates as you add or remove items. - Because arrays are objects, assigning them to new variables only copies the reference pointer, not the data.
Done with this concept?
Mark it complete to track your progress. No login needed.