Skip to content
Arrays & ObjectsChapter 20 / 47Beginner4 min read

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 variablesOne array
product1, product2, product3products list
Hard to keep countlength tells you
Cannot run a loopLoop over every item with for of
New item = new variableAdd a new item with push

3. Where is it used?

AreaWhat the array holds
E-commerceCart items, product list
Social appsPosts, comments, notifications
FormsSelected checkboxes, uploaded files
APIsJSON lists coming from the server
ReactRendering 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.

Array structure
const fruits = apple, banana, mango
Index 0 = apple, 1 = banana, 2 = mango
length = 3
Last item = index length minus 1, or at(-1)

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.

TaskChanges the original (mutating)New array (ES2023+)
Sortsort()toSorted()
Reversereverse()toReversed()
Remove / insertsplice()toSpliced()
Replace one indexarr[i] = xwith(i, x)

5. Code example

Creating, reading, and changing

js
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

js
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?

PartMeaning
push / popAdd / remove at the end
unshift / shiftAdd / remove at the start
cart.at(-1)The last item, supports negative indexes
includes / indexOfWhether an item exists, and at which index
for...ofLoop 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

Suggested order
Loops
Arrays
Array Methods
Objects
Destructuring

Final quick revision

PointDetail
IndexStarts at 0
Last itemarr.at(-1)
Add / removepush, pop, shift, unshift
Searchincludes, indexOf
Safe copiestoSorted, toReversed, toSpliced, with
Array checkArray.isArray()