JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsjavascriptArrays
Next
javascript
beginner
9 min read

Arrays

arrays
filter
functional-programming
fundamentals
map
reduce

Understanding arrays in javascript

Key Points

1map()

- Transforms each element using a callback function

2filter()

- Tests each element with a callback that returns boolean

3reduce()

- Executes a reducer function on each element

What You'll Learn

  • Understand the difference between map(), filter(), and reduce() array methods

Deep Dive

JavaScript provides three essential array methods for functional programming: map(), filter(), and reduce(). These methods process arrays without mutating the original, returning new arrays or values instead.

map()

  • Creates a new array by calling a function on every element
  • Always returns an array of the same length as the original
  • Does not mutate the original array
  • Skips empty slots in sparse arrays
  • Use when you need to transform each element (e.g., doubling numbers, extracting properties)
  • Common anti-pattern: calling map() without using the returned array — use forEach() for side effects instead

filter()

  • Creates a new array with elements that pass a test (callback returns truthy)
  • May return fewer elements than the original (or an empty array)
  • Does not mutate the original array
  • Use when you need to select or exclude items based on a condition

reduce()

  • Executes a reducer callback on each element, accumulating into a single value
  • Callback receives: (accumulator, currentValue, currentIndex, array)
  • If no initial value is provided, the first element is used as the accumulator and iteration starts from index 1
  • Calling reduce() on an empty array without an initial value throws a TypeError
  • Use when you need to compute a single result from an array (sums, averages, grouping, flattening)

Chaining

These methods return arrays (except reduce()), so they can be chained: arr.filter(...).map(...).reduce(...). This enables a declarative, pipeline-style approach to data transformation.

Working with Objects

These are Array.prototype methods. To use them on objects, convert first with Object.keys(), Object.values(), or Object.entries().

Key Interview Distinction: map() vs forEach() map() returns a new array and is chainable. forEach() returns undefined and cannot be chained. Use map() for transformations, forEach() for side effects.

Fun Fact

JavaScript's Array.prototype was dramatically expanded in ES5 (2009) with map, filter, reduce, forEach, and others — before that, developers relied on for loops for everything. Douglas Crockford's 'JavaScript: The Good Parts' (2008) didn't even mention these methods because they didn't exist yet.

Practice What You Learned

What is the difference between map(), filter(), and reduce() array methods?
junior
arrays
map() transforms each element and returns a new array of the same length, filter() returns elements that pass a test, and reduce() accumulates array elements into a single value.
What are polyfills and how do you implement Array.prototype.map from scratch?
junior
polyfills
A polyfill is code that provides modern functionality in older environments that don't natively support it. Implementing Array.prototype.map requires iterating over the array, calling the callback with each element, index, and the original array, and collecting results into a new array.
Next
Promises & Async/Await
Next