Facebook Pixel

map, filter, and reduce Interview Questions in JavaScript

These methods are asked in almost every JS interview. Here are the most common questions.

map, filter, and reduce Interview Questions in JavaScript

map, filter, and reduce are asked in almost every JavaScript interview. Here are the most common questions.

Q1: What is the difference between map and forEach?

map returns a new array (use for transformation). forEach returns undefined (use for side effects). Do not use map if you do not need the return value.

Q2: What is the difference between filter and find?

filter returns all matching elements (array). find returns the first match (single value or undefined).

Q3: Implement a polyfill for map.

Array.prototype.myMap = function (callback) { const result = []; for (let i = 0; i < this.length; i++) { result.push(callback(this[i], i, this)); } return result; }; `` ### Q4: Implement a polyfill for filter. ```js Array.prototype.myFilter = function (callback) { const result = []; for (let i = 0; i < this.length; i++) { if (callback(this[i], i, this)) result.push(this[i]); } return result; }; `` ### Q5: Implement a polyfill for reduce. ```js Array.prototype.myReduce = function (callback, initialValue) { let acc = initialValue; let start = 0; if (initialValue === undefined) { if (this.length === 0) throw new TypeError("Reduce of empty array with no initial value"); acc = this[0]; start = 1; } for (let i = start; i < this.length; i++) { acc = callback(acc, this[i], i, this); } return acc; }; `` ### Q6: What is the output? ```js ["1", "2", "3"].map(parseInt); `` **Answer**: `[1, NaN, NaN]`. `map` passes `(element, index, array)`. `parseInt` takes `(string, radix)`. So `parseInt("2", 1)` (radix 1 is invalid) is `NaN`, and `parseInt("3", 2)` (radix 2, "3" is invalid) is `NaN`. **Fix**: `["1", "2", "3"].map((s) => parseInt(s, 10))`. ### Q7: What is the output? ```js [1, 2, 3].reduce((acc, n) => acc + n); `` **Answer**: `6`. No initial value, so the first element (`1`) is the accumulator. ### Q8: What is the output? ```js [].reduce((acc, n) => acc + n); `` **Answer**: `TypeError: Reduce of empty array with no initial value`. Always pass an initial value. ### Q9: Use reduce to flatten an array. ```js [[1, 2], [3, 4]].reduce((acc, sub) => acc.concat(sub), []); // [1, 2, 3, 4] `` ### Q10: Use reduce to group objects by a property. ```js const grouped = people.reduce((acc, p) => { (acc[p.city] ||= []).push(p); return acc; }, {}); `` ### The Takeaway `map`/`filter`/`reduce` interview questions test: `map` vs `forEach`, `filter` vs `find`, polyfill implementations, the `parseInt` trap (`map(parseInt)` returns `[1, NaN, NaN]`), `reduce` without initial value, empty array `reduce` (TypeError), flattening with `reduce`, and grouping with `reduce`.

[1, NaN, NaN]. map passes (element, index, array) to the callback. parseInt takes (string, radix). So parseInt('2', 1) (radix 1 is invalid) is NaN, and parseInt('3', 2) (radix 2, '3' is invalid) is NaN. Fix with .map(s => parseInt(s, 10)).

map returns a new array (use for transformation). forEach returns undefined (use for side effects). Do not use map if you do not need the return value.

filter returns all matching elements as an array (possibly empty). find returns the first matching element (or undefined). Use filter for all matches; use find for one.

Because without an initial value, reduce uses the first element as the accumulator. An empty array has no first element, so it throws TypeError: Reduce of empty array with no initial value. Always pass an initial value.

Add a method to Array.prototype that loops through the array, calls the callback with (element, index, array), pushes the result to a new array, and returns it. Use let in the loop.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.