JavaScript Array flatMap() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.flatMap(callback) maps each element, then flattens the result by exactly one level. Think map(…).flat(1) in one step (ES2019). It returns a new array and does not mutate the source.

Remember
array.flatMap(callback)

callback returns [a, b] → both items join the result
callback returns []     → drop that element
callback returns value  → kept as one item (not nested)

≈ map(callback).flat(1)
only one flatten level — not Infinity

Syntax

Call array.flatMap. Start with this line.

JavaScript
array.flatMap(callback, thisArg?)
CallWhat it does
array.flatMap(callback, thisArg?)Primary signature.
array.flatMap(...)Call with the arguments your case needs.

Built into JavaScript. Call flatMap(...) on an array instance — no import required.

Library declarations
JavaScript
array.flatMap(callback, thisArg?)

Parameters

ParameterDescription
ArrayRequired. Source to map over
callbackRequired. Return a value or array per item
thisArgOptional. Optional this for the callback

Return Value

Return ValueDescription
*New array, flattened one level

JavaScript Examples

Example 1 — Map Each Item to Several Values

Return an array from the callback; those items join the result.

JavaScript
const numbers = [1, 2, 3];

const expanded = numbers.flatMap((n) => [n, n * 2]);

console.log(expanded);
// [1, 2, 2, 4, 3, 6]
Try It Yourself

How It Works

  1. Callback per item. Each number returns [n, n * 2].
  2. One-level flatten. Those small arrays are unwrapped into one list.
  3. Order preserved. Results appear in source order.
  4. New array. numbers is unchanged.

Example 2 — flatMap() vs map()

map keeps nesting; flatMap unwraps one level.

JavaScript
const numbers = [1, 2, 3];
const pair = (n) => [n, n * 10];

console.log(numbers.map(pair));
// [[1, 10], [2, 20], [3, 30]]

console.log(numbers.flatMap(pair));
// [1, 10, 2, 20, 3, 30]
Try It Yourself

How It Works

  1. Same callback. Both produce [n, n * 10] per item.
  2. map. Keeps each returned array as one element → nested result.
  3. flatMap. Unwraps that one level → a single flat list.
  4. Shortcut. Prefer flatMap over map(…).flat().

Example 3 — Filter by Returning []

Drop items with an empty array; keep others with [value].

JavaScript
const numbers = [1, 2, 3, 4, 5];

const evensDoubled = numbers.flatMap((n) =>
  n % 2 === 0 ? [n * 2] : []
);

console.log(evensDoubled);
// [4, 8]
Try It Yourself

How It Works

  1. Even → keep. Return [n * 2] so the doubled value joins the result.
  2. Odd → drop. Return []; nothing is added for that item.
  3. One pass. Filter and map happen together.
  4. Alternative. filter then map also works; flatMap is a compact option.

Browser & Runtime Support

Array.prototype.flatMap() is part of ES2019, shipped with flat(). It is available in evergreen browsers and Node.js 11+.

✓ Baseline · ES2019

Array.prototype.flatMap()

Supported in Chrome 69+, Firefox 62+, Safari 12+, Edge 79+, and Node 11+. Not available in Internet Explorer.

97% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.flatMap() Excellent

Bottom line: Safe for modern web apps and current Node releases. For IE11, feature-detect with if ("flatMap" in Array.prototype) and fall back to arr.map(fn).flat() or a polyfill.

Frequently Asked Questions

flatMap() runs a map callback on each element, then flattens the result by exactly one level. It is similar to map(callback).flat(1), with slightly different sparse-array behavior.
map() keeps nested arrays in the output. flatMap() merges one level of nesting so you get a single flat list when your callback returns arrays.
flat() only flattens existing nesting. flatMap() first transforms each element with a callback, then flattens one level of whatever the callback returns.
No. flatMap() always returns a new array. The source array is not changed.
Yes. Return an empty array [] to drop an element, or [value] to keep one. This is a common filter-and-map pattern in one pass.
flatMap() is ES2019. It works in all modern browsers and Node.js 11+. Internet Explorer does not support it.

Next: forEach()

Learn how to run a side-effect for every array element.

forEach() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful