ForgeFrontend — Prepare, Practice, Crack
Secure checkout
Lifetime access
Instant Drive delivery
Free updates forever

Prepare · Practice · Crack

Which array methods mutate, and what's the sort() gotcha?

Quick answer

push/pop/shift/unshift/splice/sort/reverse mutate; map/filter/slice/concat don't. sort() is lexicographic unless you pass a comparator.

In detail

Knowing which methods mutate prevents bugs, especially in React where state must not be mutated. The classic trap: `sort()` converts items to strings, so `[10, 2, 1].sort()` gives `[1, 10, 2]`. Always sort numbers with `(a, b) => a - b`, and copy first with spread if you must keep the original.

[10, 2, 1].sort();              // [1, 10, 2]  (string order!)
[10, 2, 1].sort((a, b) => a - b); // [1, 2, 10]
const sorted = [...arr].sort();  // copy then sort

⚠️Gotcha

Default sort is alphabetical. Always pass (a,b)=>a-b for numbers.

Why interviewers ask this: Mutation + sort are frequent real bugs.

This is 1 of 118+ questions in the JavaScript Interview Kit

Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.

⚡ Get the JavaScript Interview Kit → ₹299

Full kit

JavaScript Interview Kit · ₹299

Get it →