Arrays · Lesson 15 of 40
JavaScript Arrays: Create, Add, Remove, Copy
An array is an ordered list of values: products in a cart, messages in a chat, rows from a database. Arrays are the data structure you'll use most in JavaScript, and they come with dozens of methods. The single most useful thing to learn first is which methods change the array and which return a new one – mixing them up causes real bugs, especially in React.
In this lesson
- Creating arrays and reading items
- Adding and removing: push, pop, shift, unshift, splice
- Mutating vs non-mutating methods
- Spread, destructuring, and shallow vs deep copies
Creating and reading
const cart = ["Notebook", "Pen", "Bag"];
const mixed = [42, "text", true, null, { id: 1 }, [1, 2]]; // any types allowed
console.log(cart.length, cart[0], cart.at(-1));
console.log(cart[5]); // missing index: undefined
console.log(Array.from({ length: 5 }, (_, i) => i * 10));
console.log(Array(3).fill(0), Array.of(7));
console.log(Array.from("abc"), [..."hey"]);
console.log(Array.isArray(cart), typeof cart);
3 Notebook Bag undefined [ 0, 10, 20, 30, 40 ] [ 0, 0, 0 ] [ 7 ] [ 'a', 'b', 'c' ] [ 'h', 'e', 'y' ] true object
Adding and removing
const queue = ["Asha", "Ravi"];
queue.push("Meera"); // add to the end → returns new length
queue.unshift("Zoya"); // add to the start
console.log(queue);
const last = queue.pop(); // remove from the end
const first = queue.shift(); // remove from the start
console.log(first, last, queue);
[ 'Zoya', 'Asha', 'Ravi', 'Meera' ] Zoya Meera [ 'Asha', 'Ravi' ]
Where each method works
- unshift / shiftat the start – slower on big arrays (every index moves)
- splice(index, deleteCount, …items)anywhere in the middle
- push / popat the end – fast; arrays work as a stack
splice: remove or insert anywhere
const steps = ["plan", "code", "deploy"];
steps.splice(2, 0, "test"); // at index 2, delete 0, insert "test"
console.log(steps);
const removed = steps.splice(1, 1); // at index 1, delete 1
console.log(removed, steps);
steps.splice(1, 1, "review", "QA"); // replace 1 item with 2
console.log(steps);
[ 'plan', 'code', 'test', 'deploy' ] [ 'code' ] [ 'plan', 'test', 'deploy' ] [ 'plan', 'review', 'QA', 'deploy' ]
Mutating vs non-mutating
| Changes the original (mutating) | Returns a new array (non-mutating) |
|---|---|
push, pop, shift, unshift | concat, spread [...a, x] |
splice | slice, toSpliced |
sort, reverse | toSorted, toReversed |
fill, copyWithin, arr[i] = x | with(i, x), map, filter |
const marks = [72, 95, 60];
const sorted = marks.toSorted((a, b) => a - b); // ES2023: copy, then sort
const updated = marks.with(1, 99); // copy with one item replaced
const firstTwo = marks.slice(0, 2); // slice never mutates
console.log(marks, sorted, updated, firstTwo);
[ 72, 95, 60 ] [ 60, 72, 95 ] [ 72, 99, 60 ] [ 72, 95 ]
Why this matters in React and state management
UI libraries detect changes by comparing references. items.push(x) keeps the same array, so the UI may not update; setItems([...items, x]) creates a new one. Prefer non-mutating methods for any state.
Spread and destructuring
const frontend = ["HTML", "CSS"];
const backend = ["Node", "SQL"];
const fullStack = [...frontend, "JavaScript", ...backend];
console.log(fullStack);
const [top, second, ...rest] = [98, 91, 85, 70, 64];
console.log(top, second, rest);
const [, , third = "none"] = ["gold", "silver"]; // skip items, default value
console.log(third);
[ 'HTML', 'CSS', 'JavaScript', 'Node', 'SQL' ] 98 91 [ 85, 70, 64 ] none
Shallow vs deep copies
const orders = [{ id: 1, status: "new" }, { id: 2, status: "new" }];
const shallow = [...orders]; // new array, SAME objects inside
shallow[0].status = "shipped";
console.log(orders[0].status); // original object changed too!
const deep = structuredClone(orders); // copies nested objects as well
deep[1].status = "cancelled";
console.log(orders[1].status, deep[1].status);
shipped new cancelled
What a shallow copy shares
orders[ →#1, →#2 ]array Ashallow[ →#1, →#2 ]array B – same objectsdeep[ →#3, →#4 ]array C – new objects
Common mistake: const copy = original
That copies the reference, not the array – both names point at the same list. Use spread or slice() for a shallow copy and structuredClone() when nested objects must be independent.
Practice
Try it yourself
Remove "Pen" from
["Notebook", "Pen", "Bag"]without mutating the original.Show solution
const cart = ["Notebook", "Pen", "Bag"]; const i = cart.indexOf("Pen"); const without = cart.toSpliced(i, 1); // or cart.filter((x) => x !== "Pen") console.log(cart, without);[ 'Notebook', 'Pen', 'Bag' ] [ 'Notebook', 'Bag' ]
Quick check
slice vs splice?
slice(start, end) returns a copy of part of the array and never mutates; splice removes/inserts items in place.
Does [...arr] copy nested objects?
No – it's a shallow copy; use structuredClone for a deep copy.
Which method sorts without changing the original?
toSorted() (ES2023), or [...arr].sort().
Summary
- push/pop work at the end, shift/unshift at the start, splice anywhere.
- Know which methods mutate; prefer toSorted, with, slice, spread for state.
- Spread makes shallow copies; structuredClone makes deep copies.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.