Top JS Array Methods Interview Questions to Know
The array method questions that come up most often in JavaScript interviews, answered directly with short examples: map vs forEach, mutating vs non-mutating methods, reduce, and more.
This article answers the JavaScript array interview questions that come up most often, covering the array methods candidates are actually asked about. Each question is answered directly with a short example, since interviewers care more about whether you can explain the behavior than whether you can recite a definition.
Here is one of the answers up front, since it sets up the pattern for the rest of the article: mapping over an array builds a brand new array and leaves the original completely untouched.
const scores = [10, 20, 30];
const doubled = scores.map((score) => score * 2);
console.log(doubled);
console.log(scores);
// [ 20, 40, 60 ]
// [ 10, 20, 30 ]The new array holds the doubled values, and the original scores array still holds the numbers it started with. The rest of this article works through the other questions that follow the same pattern of explaining return values and mutation.
What Is the Difference Between map and forEach?
Mapping returns a new array built from the callback's return value on each item. Looping with forEach returns undefined and exists only to run code for each item, such as logging or updating something outside the array:
const numbers = [1, 2, 3];
const mapped = numbers.map((n) => n * 10);
const loopResult = numbers.forEach((n) => n * 10);
console.log(mapped);
console.log(loopResult);
// [ 10, 20, 30 ]
// undefinedIf you need a transformed array back, map is the right tool. If you only need to perform an action, like logging each item, forEach is enough on its own.
Calling map and throwing away its result is a common mistake interviewers ask about, since it works but wastes the array it creates. See map vs forEach: which should you use for more detail.
Which Array Methods Mutate the Original Array?
This is one of the most frequently asked array questions, since mixing up mutating and non-mutating methods is a real source of bugs in production code.
| Mutates the original | Does not mutate |
|---|---|
| push, pop, shift, unshift | map, filter, slice |
| splice | concat |
| sort, reverse | flat, flatMap |
Sorting is the method that surprises the most people, since it changes the array in place instead of returning a fresh one, as shown below:
const letters = ["b", "a", "c"];
const sorted = letters.sort();
console.log(sorted === letters);
console.log(letters);
// true
// [ 'a', 'b', 'c' ]Sorting reorders the letters array in place and returns that same array reference, which is why the equality check comes back true. This surprises developers who expect it to behave like mapping. If the original array needs to stay untouched, copy it with the spread operator before sorting.
How Does reduce Work, and What Can It Replace?
Reduce runs a callback over every item, carrying an accumulator value forward, and returns a single final value instead of an array:
const prices = [12, 25, 8];
const total = prices.reduce((sum, price) => sum + price, 0);
console.log(total);
// 45The second argument, a zero here, is the starting value for the accumulator. Reduce is flexible enough to implement mapping, filtering, or a running total in one method, which is why interviewers often ask candidates to rebuild map using reduce to check they understand the accumulator pattern. See the JavaScript Array reduce method for a full breakdown.
What Is the Difference Between slice and splice?
Slicing returns a shallow copy of a portion of an array without changing the original. Splicing removes or inserts items directly into the original array and returns whatever it removed:
const items = ["a", "b", "c", "d"];
const copy = items.slice(1, 3);
const removed = items.splice(1, 2);
console.log(copy);
console.log(items);
// [ 'b', 'c' ]
// [ 'a', 'd' ]Both calls return the same two letters in this example, but only splicing changes the items array afterward. This name similarity makes it one of the most commonly confused pairs in interviews. See slice vs splice: what is the difference for more examples.
How Do You Check If Every or Some Items Match a Condition?
Checking every item requires the callback to return truthy for all of them. Checking some items only requires the callback to return truthy for at least one:
const ages = [22, 17, 30];
console.log(ages.every((age) => age >= 18));
console.log(ages.some((age) => age >= 18));
// false
// trueThe first check stops at the first item that fails the condition, and the second stops at the first item that passes it, so neither method always scans the full array. This short-circuit behavior is worth mentioning if asked about performance on large arrays.
What Is the Difference Between find and filter?
Finding returns the first matching item, or undefined if nothing matches. Filtering returns an array of every matching item, or an empty array if nothing matches at all:
const users = [
{ id: 1, active: false },
{ id: 2, active: true },
{ id: 3, active: true },
];
console.log(users.find((u) => u.active));
console.log(users.filter((u) => u.active));
// { id: 2, active: true }
// [ { id: 2, active: true }, { id: 3, active: true } ]Use finding when exactly one match is expected, such as looking up a record by id. Use filtering when there can be several matches and every one of them is needed.
How Do You Flatten a Nested Array?
Flattening collapses nested arrays into a single level. A combined map-and-flatten method maps each item first, then flattens the result by one level, which is more efficient than calling the two separately:
const nested = [[1, 2], [3, 4], [5]];
console.log(nested.flat());
const words = ["hello world", "foo bar"];
console.log(words.flatMap((sentence) => sentence.split(" ")));
// [ 1, 2, 3, 4, 5 ]
// [ 'hello', 'world', 'foo', 'bar' ]Flattening with no argument only goes one level deep. For deeply nested arrays, pass a depth number, or use Infinity as the depth to flatten completely regardless of how deep the nesting goes.
Common Mistakes to Avoid
- Calling map when only side effects are needed, which wastes the array it builds. Use forEach instead.
- Assuming sort returns a new array. It sorts and returns the same array reference.
- Confusing slice, a copy with no mutation, with splice, which mutates and returns removed items.
- Forgetting the initial value in reduce, which changes what the first callback call receives as its accumulator.
- Using find when every match is actually needed, which silently returns just the first result.
Practicing These Concepts
These questions test understanding of return values and mutation more than memorization. Practice by predicting the output of a method before running it, then check yourself against the console. For hands-on practice with another topic that comes up often, see removing duplicates from arrays in JavaScript.
Rune AI
Key Insights
- map returns a new array of the same length; forEach returns undefined and is used only for side effects.
- push, pop, splice, sort, and reverse mutate the original array; map, filter, slice, and concat do not.
- reduce can implement map, filter, and a running total in one method, since it folds an array down to any single value.
- flat and flatMap handle nested arrays that map and filter cannot flatten on their own.
- Knowing what a method returns and whether it mutates matters more in an interview than memorizing every method name.
Frequently Asked Questions
Do interviewers expect me to memorize every array method?
Should I always prefer non-mutating methods in interview answers?
Conclusion
Array method questions test whether you understand return values, mutation, and callback behavior, not whether you have memorized every method's exact signature. Knowing which methods mutate, what each one returns, and why reduce can replace several other methods covers most of what comes up in a JavaScript interview.
More in this topic
Using Reflect and Proxy Together in JavaScript
Proxy traps intercept operations, but Reflect is what makes them behave correctly. Learn why Reflect belongs inside every trap and what breaks when you skip it.
JavaScript Reflect API: Advanced Architecture
The Reflect object exposes JavaScript's own internal operations as plain functions. Learn every Reflect method, what it returns, and why it exists as its own API.
JS Metaprogramming: Advanced Architecture Guide
Metaprogramming means writing code that inspects or changes how other code behaves. See how Proxy, Reflect, Symbol, and property descriptors work together as JavaScript's metaprogramming toolkit.