Arrays
Learn JavaScript array basics, including creating arrays, index access, at(), push, pop, includes, loops, and non-mutating methods like toSorted.
On this page
1. What are arrays?
Every item in an array has a number called an index, and the index starts at 0. An array can hold numbers, strings, objects, and even other arrays.
In JavaScript, an array is actually a special kind of object, which is why typeof [] returns "object". To check for an array, use Array.isArray().
2. Why does it matter?
If you have 50 products, creating 50 separate variables is impossible. An array keeps the whole list under one name.
| Separate variables | One array |
|---|---|
| product1, product2, product3 | products list |
| Hard to keep count | length tells you |
| Cannot run a loop | Loop over every item with for of |
| New item = new variable | Add a new item with push |
3. Where is it used?
| Area | What the array holds |
|---|---|
| E-commerce | Cart items, product list |
| Social apps | Posts, comments, notifications |
| Forms | Selected checkboxes, uploaded files |
| APIs | JSON lists coming from the server |
| React | Rendering a list with items.map(...) |
4. How does it work?
An array keeps values in a line. length is always one more than the last index.
Mutating vs non-mutating
Some methods change the original array, while others return a new array. This difference matters a lot in React and state management.
| Task | Changes the original (mutating) | New array (ES2023+) |
|---|---|---|
| Sort | sort() | toSorted() |
| Reverse | reverse() | toReversed() |
| Remove / insert | splice() | toSpliced() |
| Replace one index | arr[i] = x | with(i, x) |
5. Code example
Creating, reading, and changing
const cart = ["shirt", "shoes"];
cart.push("cap");
cart.unshift("watch");
console.log(cart);
console.log(cart[0]);
console.log(cart.at(-1));
console.log(cart.length);
const removed = cart.pop();
console.log(removed, cart.includes("shoes"), cart.indexOf("shirt"));
for (const item of cart) {
console.log(item);
}Non-mutating methods
const scores = [72, 95, 40];
const sorted = scores.toSorted((a, b) => b - a);
const updated = scores.with(1, 99);
console.log(scores);
console.log(sorted);
console.log(updated);
const ids = Array.from({ length: 3 }, (_, i) => i + 1);
console.log(ids, Array.isArray(ids));How does the code work?
| Part | Meaning |
|---|---|
push / pop | Add / remove at the end |
unshift / shift | Add / remove at the start |
cart.at(-1) | The last item, supports negative indexes |
includes / indexOf | Whether an item exists, and at which index |
for...of | Loop directly over each value |
toSorted((a, b) => b - a) | A new array, largest to smallest, scores stays the same |
with(1, 99) | A new array with index 1 replaced |
Array.from(...) | Generated [1, 2, 3] |
Interview tip
const does not freeze an array. After const cart = [], cart.push() still works. You just cannot assign cart = [] again.
6. Common mistakes
Mistake: Thinking the index starts at 1
Why? The first item is at index 0, and arr[arr.length] is always undefined.
Better: Use arr[0] for the first item, and arr.at(-1) or arr[arr.length - 1] for the last.
Mistake: Sorting numbers with sort() and no compare function
Why? By default, sort converts values to strings before comparing, so [10, 9, 1].sort() gives [1, 10, 9].
Better: Pass a compare function: toSorted((a, b) => a - b).
Mistake: Mutating a React state array directly
Why? push or sort keeps the same array reference, so React may miss the re-render.
Better: Create a new array with [...items, newItem], toSorted(), toSpliced(), or with().
Mistake: Using typeof to check for an array
Why? typeof [] also returns object, so you cannot tell an array from an object.
Better: Use Array.isArray(value).
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Data Types, Loops |
| Alongside | Objects, Destructuring |
| After | Array Methods: map, filter, reduce, Spread & Rest, Map & Set |
Final quick revision
| Point | Detail |
|---|---|
| Index | Starts at 0 |
| Last item | arr.at(-1) |
| Add / remove | push, pop, shift, unshift |
| Search | includes, indexOf |
| Safe copies | toSorted, toReversed, toSpliced, with |
| Array check | Array.isArray() |