Autocoder

JavaScript Array Methods Interview Questions

Last updated 2026-11-19

Array methods feature in almost every JavaScript screening because they reveal whether you think in transformations. Expect to choose between map, filter and reduce, to know which methods mutate the array, and to implement a polyfill for one of them.

The answers below list the behaviour interviewers probe: return values, mutation, sparse arrays, the default sort order and callback arguments. Output comments were checked against current Node.js.

LevelFresher to 3 years
FocusIteration, transformation, mutation, polyfills
RuntimeBrowser and Node.js 18 or later

Mutating or not

Knowing which methods change the original array is a staple question and a source of real bugs, especially in React state.

Related pages

Array questions often sit inside coding problems; practise them in JavaScript coding questions and on the array interview questions page. The wider plan is in how to crack a JavaScript interview.

JavaScript Array Methods Interview Questions: questions with worked answers

  1. 1

    What is the difference between map, filter, reduce and forEach?

    easy

    map returns a new array of the same length with each element transformed. filter returns a new array with the elements whose callback is truthy. reduce folds the array into a single value of any type using an accumulator. forEach just runs a function for each element and returns undefined, so it cannot be chained and is meant for side effects.

    javascript
    const n = [1, 2, 3, 4];
    console.log(n.map((x) => x * 2));            // [ 2, 4, 6, 8 ]
    console.log(n.filter((x) => x % 2 === 0));   // [ 2, 4 ]
    console.log(n.reduce((a, x) => a + x, 0));   // 10
    console.log(n.forEach((x) => x));            // undefined
  2. 2

    What does reduce do without an initial value, and what happens on an empty array?

    medium

    Without an initial value, the first element is the starting accumulator and iteration begins at the second element. On an empty array with no initial value, reduce throws a TypeError. Always pass an initial value, especially when the result type differs from the elements.

    javascript
    console.log([5].reduce((a, b) => a + b));        // 5 (callback never runs)
    try { [].reduce((a, b) => a + b); } catch (e) { console.log(e.name); }  // TypeError
    console.log([].reduce((a, b) => a + b, 0));      // 0
  3. 3

    What is the difference between slice and splice?

    medium

    slice(start, end) returns a shallow copy of a portion without changing the original, with end excluded. splice(start, deleteCount, ...items) mutates the array, removing and optionally inserting elements, and returns the removed elements. Negative indices count from the end for both.

    javascript
    const a = [1, 2, 3, 4, 5];
    console.log(a.slice(1, 3), a);        // [ 2, 3 ] [ 1, 2, 3, 4, 5 ]
    console.log(a.splice(1, 2, "x"), a);  // [ 2, 3 ] [ 1, 'x', 4, 5 ]
    console.log(a.slice(-2));             // [ 4, 5 ]
  4. 4

    Predict the output: Array(3).map and sparse arrays.

    hard

    Array(3) creates three empty slots, not three undefined values. map, forEach and filter skip empty slots, so the result still has holes. Use Array.from with a map function, or fill first, to create real values.

    javascript
    console.log(Array(3).map((_, i) => i));          // [ <3 empty items> ]
    console.log(Array.from({ length: 3 }, (_, i) => i));   // [ 0, 1, 2 ]
    console.log([...Array(3).keys()]);               // [ 0, 1, 2 ]
    console.log(Array(3).fill(0));                   // [ 0, 0, 0 ]
  5. 5

    Predict the output: Array(3).fill([]) and shared references.

    hard

    fill puts the same value into every slot. With an object or array the slots all refer to one object, so mutating one changes all. Build independent rows with Array.from and a callback.

    javascript
    const g = Array(2).fill([]);
    g[0].push(1);
    console.log(g);                                  // [ [ 1 ], [ 1 ] ]
    const h = Array.from({ length: 2 }, () => []);
    h[0].push(1);
    console.log(h);                                  // [ [ 1 ], [] ]
  6. 6

    What is the difference between includes and indexOf, and between find and findIndex?

    medium

    includes returns a boolean and finds NaN, using SameValueZero. indexOf returns the position, or -1 if absent, and uses strict equality, so it cannot find NaN. find returns the first element for which the callback is truthy, or undefined, and findIndex returns its index or -1. some returns whether any element passes and every whether all do.

    javascript
    console.log([NaN].includes(NaN), [NaN].indexOf(NaN));   // true -1
    console.log([1, 5, 8].find((x) => x > 4));              // 5
    console.log([1, 5, 8].findIndex((x) => x > 9));         // -1
    console.log([].every(() => false), [].some(() => true));// true false
  7. 7

    Why does sort need a comparator for numbers, and is it stable?

    hard

    Without a comparator, sort converts items to strings and orders them by UTF-16 code units, so 10 comes before 9. A comparator returning a negative, zero or positive number defines the order, and (a, b) => a - b sorts numbers ascending. sort mutates and returns the same array. Since ES2019 the sort is stable, so equal elements keep their order. Use toSorted for a copy where supported.

    javascript
    console.log([3, 20, 100].sort());                    // [ 100, 20, 3 ]
    console.log([3, 20, 100].sort((a, b) => a - b));     // [ 3, 20, 100 ]
    const people = [{ n: "a", age: 30 }, { n: "b", age: 25 }, { n: "c", age: 30 }];
    console.log(people.sort((x, y) => x.age - y.age).map((p) => p.n).join(""));  // bac
  8. 8

    How do flat and flatMap work?

    medium

    flat(depth) concatenates nested arrays up to the given depth, default 1, and Infinity flattens fully. flatMap maps and then flattens by one level in a single pass, handy for returning zero, one or many items per input, which also lets it act as filter and map together.

    javascript
    console.log([1, [2, [3, [4]]]].flat());          // [ 1, 2, [ 3, [ 4 ] ] ]
    console.log([1, [2, [3, [4]]]].flat(Infinity));  // [ 1, 2, 3, 4 ]
    console.log([1, 2, 3].flatMap((x) => (x % 2 ? [x, x * 10] : [])));  // [ 1, 10, 3, 30 ]
  9. 9

    Group an array of objects by a property using reduce.

    medium

    Use reduce with an object accumulator. For each item, compute the key, create the bucket if missing and push. This is O(n). Newer runtimes provide Object.groupBy and Map.groupBy for the same task.

    javascript
    const items = [{ t: "a", v: 1 }, { t: "b", v: 2 }, { t: "a", v: 3 }];
    const grouped = items.reduce((acc, it) => {
      (acc[it.t] ||= []).push(it.v);
      return acc;
    }, {});
    console.log(grouped);   // { a: [ 1, 3 ], b: [ 2 ] }
  10. 10

    Write a polyfill for map.

    hard

    Add the method to Array.prototype, validate the callback, loop over indices that exist, call the callback with value, index and array using the optional thisArg, and place results at the same index in a new array. Skipping holes with the in operator matches the specification.

    javascript
    Array.prototype.myMap = function (cb, thisArg) {
      if (typeof cb !== "function") throw new TypeError(cb + " is not a function");
      const out = new Array(this.length);
      for (let i = 0; i < this.length; i++) {
        if (i in this) out[i] = cb.call(thisArg, this[i], i, this);
      }
      return out;
    };
    console.log([1, 2, 3].myMap((x) => x * 3));   // [ 3, 6, 9 ]
  11. 11

    Write a polyfill for reduce.

    medium

    Handle the optional initial value by checking arguments.length, not whether the value is undefined. Throw a TypeError for an empty array without an initial value, then loop applying the callback with accumulator, value, index and array.

    javascript
    Array.prototype.myReduce = function (cb, init) {
      let i = 0, acc = init;
      if (arguments.length < 2) {
        if (this.length === 0) throw new TypeError("Reduce of empty array with no initial value");
        acc = this[i++];
      }
      for (; i < this.length; i++) if (i in this) acc = cb(acc, this[i], i, this);
      return acc;
    };
    console.log([1, 2, 3].myReduce((a, b) => a + b));   // 6
  12. 12

    How do you remove duplicates and find the intersection of two arrays?

    medium

    A Set removes duplicates in O(n). For an intersection, put one array in a Set and filter the other by membership, O(n + m). Avoid filter with indexOf on large inputs because it is O(n squared).

    javascript
    const a = [1, 2, 2, 3, 4], b = [3, 4, 5];
    console.log([...new Set(a)]);                        // [ 1, 2, 3, 4 ]
    const sb = new Set(b);
    console.log([...new Set(a)].filter((x) => sb.has(x)));   // [ 3, 4 ]
  13. 13

    Predict the output: setting length and using delete on arrays.

    hard

    Assigning a smaller length truncates the array, and a larger one adds empty slots. delete arr[i] removes the element but leaves a hole, without shifting items or changing length. Use splice to remove an element and close the gap.

    javascript
    const a = [1, 2, 3, 4];
    delete a[1];
    console.log(a, a.length);     // [ 1, <1 empty item>, 3, 4 ] 4
    a.length = 2;
    console.log(a);               // [ 1, <1 empty item> ]
    console.log([1, 2, 3].push(4), [1, 2, 3].pop());   // 4 3
  14. 14

    How do you reverse an array without mutating it, and what are the newer copy methods?

    medium

    Spread or slice into a copy before calling reverse, or use toReversed in newer runtimes. The same pattern exists for toSorted, toSpliced and with, which return changed copies and leave the original array untouched, which helps with immutable state updates.

    javascript
    const a = [1, 2, 3];
    console.log([...a].reverse(), a);      // [ 3, 2, 1 ] [ 1, 2, 3 ]
    console.log(a.toReversed(), a.with(0, 9), a.at(-1));  // [ 3, 2, 1 ] [ 9, 2, 3 ] 3

Frequently asked questions

Does map change the original array?

No. It returns a new array. Methods that mutate the original include push, pop, splice, sort and reverse.

When should I use reduce instead of a loop?

When you are building a single result from the array and the accumulator logic is short. For complex logic, a for...of loop is usually more readable.

Why does sort give wrong results for numbers?

The default comparison is by string. Pass (a, b) => a - b for ascending numeric order.

How do I check if a value is an array?

Use Array.isArray(value). typeof returns 'object' for arrays, and instanceof can fail across frames.

Related guides

More in Programming Language Interview Questions by Language

See all in this section