Array in JavaScript: Methods, Copies & Traps
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(','));45const holes = new Array(3);6console.log(holes.length, holes.map(() => 'x').join('-'), Array.from({ length: 3 }, (_, i) => i).join(','));78console.log(typeof nums, Array.isArray(nums));9console.log([NaN].indexOf(NaN), [NaN].includes(NaN));1011const copy = nums.slice();12copy.push(99);13console.log(nums.length, copy.length);
Predict Console Output
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'.
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.
- Mutating vs non-mutating:
push,pop,shift,unshift,splice,sort,reverseandfillchange the array itself.slice,concat,map,filter, the spread[...arr]and the ES2023toSorted,toReversedandtoSplicedreturn new arrays. - Default sort is by string:
[10, 1, 5].sort()gives[1, 10, 5]. Pass a comparator for numbers:arr.sort((a, b) => a - b). - Empty slots and searching:
new Array(3)has length 3 but no elements, andmap/forEachskip the holes;Array.from({ length: 3 }, fn)orArray(3).fill(0)create real values.indexOfcannot findNaN;includescan.
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.
How do you check whether a value is an array?
Use Array.isArray(value). typeof returns 'object' for arrays, and instanceof Array fails for arrays created in another frame.
Why does [10, 1, 5].sort() give [1, 10, 5]?
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.
Which array methods change the original array?
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.
What is the difference between indexOf and includes?
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.
What are empty slots in an array?
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.
How do you copy an array?
[...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
