JavaScript Array Interview Questions
Array methods are daily drivers — and a favourite interview topic. Here are the questions and clear answers.
map vs forEach?
map returns a new transformed array and is chainable; forEach returns undefined and is for side effects. Use map to transform, forEach for side effects.
How does reduce work?
It folds an array into a single value with an accumulator — sums, objects, grouping, anything. Always pass an initial value to avoid edge cases on empty arrays.
[1,2,3].reduce((s,n)=>s+n, 0); // 6Which array methods mutate?
Mutating: push, pop, shift, unshift, splice, sort, reverse. Non-mutating: map, filter, slice, concat, spread. This matters in React, where state must not be mutated.
What's the sort() gotcha?
sort() converts items to strings by default, so [10, 2, 1].sort() gives [1, 10, 2]. Always pass a comparator for numbers: sort((a, b) => a - b).
How do you remove duplicates from an array?
Wrap it in a Set and spread back: [...new Set(arr)]. For objects, dedupe by a key using a Map or a seen-Set.
Master arrays for interviews
map/filter/reduce, mutation gotchas and array coding problems — in the JavaScript Interview Kit.
⚡ Get the JavaScript Interview Kit → ₹299Frequently asked questions
- What's the difference between slice and splice?
- slice returns a copy of a portion without changing the original; splice mutates the array (removing/inserting) and returns removed items.
- find vs filter?
- find returns the first match (or undefined); filter returns all matches as an array.
