Array in JavaScript: Methods, Copies & Traps

Easy100% Free~12 mins#arrays#array-methods#sort#immutability#basics
Interview Answer (30-Second Summary)

An array in JavaScript is an ordered, zero-indexed list of values; it is an object with numeric keys and a length property, so typeof gives 'object' and Array.isArray is the real check. Some methods mutate it (push, splice, sort, reverse) and others return a new array (slice, map, filter, concat, toSorted).

Key Learning Objectives
✓

Create arrays with literals, Array.from and new Array, and know what empty slots are.

✓

Tell mutating methods (push, sort, splice, reverse) from non-mutating ones (slice, map, concat, toSorted).

✓

Predict the default sort order and fix it with a comparator.

✓

Pick between indexOf and includes, and check for arrays with Array.isArray.

The Interview Problem

What do the five console.log calls print?

1const nums = [10, 1, 5];
2const sorted = nums.sort();
3console.log(sorted === nums, nums.join(','));
4
5const holes = new Array(3);
6console.log(holes.length, holes.map(() => 'x').join('-'), Array.from({ length: 3 }, (_, i) => i).join(','));
7
8console.log(typeof nums, Array.isArray(nums));
9console.log([NaN].indexOf(NaN), [NaN].includes(NaN));
10
11const copy = nums.slice();
12copy.push(99);
13console.log(nums.length, copy.length);
Predict Console Output
Interactive Challenge

Select the option that matches what this code prints to the console:

true 1,10,5 / 3 -- 0,1,2 / object true / -1 true / 3 4

true 1,5,10 / 3 -- 0,1,2 / object true / -1 true / 3 4

true 1,10,5 / 3 x-x-x 0,1,2 / object true / -1 true / 3 4

true 1,10,5 / 3 -- 0,1,2 / array true / -1 true / 3 4

V8 Engine Execution Trace
Step 1 of 6 (Line 2)

sort reorders nums in place and returns the same array. With no comparator it compares string forms: '1' < '10' < '5'.

Call Stack (Top = Active)
Global
Array.prototype.sort
Lexical Scope / Bindings
nums:[1, 10, 5]
sorted:same array as nums
Console Stream
> [empty]

Array in JavaScript Explained

How an Array in JavaScript Behaves

An array in JavaScript is an ordered, zero-indexed list that can hold any values. Under the hood it is an object with numeric keys and a length property, which is why typeof [] is 'object' and Array.isArray is the check to use.

  1. Mutating vs non-mutating: push, pop, shift, unshift, splice, sort, reverse and fill change the array itself. slice, concat, map, filter, the spread [...arr] and the ES2023 toSorted, toReversed and toSpliced return new arrays.
  2. Default sort is by string: [10, 1, 5].sort() gives [1, 10, 5]. Pass a comparator for numbers: arr.sort((a, b) => a - b).
  3. Empty slots and searching: new Array(3) has length 3 but no elements, and map/forEach skip the holes; Array.from({ length: 3 }, fn) or Array(3).fill(0) create real values. indexOf cannot find NaN; includes can.
Common Traps & Mistakes

Calling sort() on numbers without a comparator.

Assuming sort or reverse return a copy and then using the original as if it were unchanged.

Using new Array(n).map(...) to build a list and getting empty slots back.

FAANG Follow-Up Probes
Probe #1

Write a function that removes duplicates from an array while keeping the first occurrence.

Probe #2

How would you sort an array of objects by two fields?

Probe #3

What is the difference between splice and slice?

Array in JavaScript Interview Questions

Key interview inquiries, edge cases, and architectural follow-ups for Arrays.

Q1.
What is an array in JavaScript?

An ordered list of values of any type, indexed from 0. It is a special object with numeric keys and an automatically updated length property.

Use Array.isArray(value). typeof returns 'object' for arrays, and instanceof Array fails for arrays created in another frame.

Without a comparator, sort converts elements to strings and compares them as text, so '10' comes before '5'. Use sort((a, b) => a - b) for numbers.

push, pop, shift, unshift, splice, sort, reverse, fill and copyWithin. slice, concat, map, filter, reduce, flat and the ES2023 toSorted, toReversed, toSpliced and with return new arrays.

indexOf returns a position and uses strict equality, so it cannot find NaN. includes returns a boolean and uses SameValueZero, so [NaN].includes(NaN) is true.

Positions that were never assigned, such as in new Array(3) or [1, , 3]. map, forEach and filter skip them; Array.from and fill create real values instead.

[...arr], arr.slice() or Array.from(arr) make a shallow copy: the new array is separate, but objects inside it are still shared. Use structuredClone for a deep copy.

More JavaScript interview questions for freshers