JavaScript Array flatMap() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Map + Flatten

What You’ll Learn

The flatMap() method maps each element with a callback, then flattens the result by one level. It is the modern replacement for map().flat(). This tutorial covers syntax, five examples, the filter-with-empty-array trick, and how flatMap() compares to map() and flat().

01

Syntax

flatMap(fn)

02

map + flat

One step

03

Depth 1

Always one level

04

Filter trick

[] or [x]

05

New array

No mutation

06

ES2019

Modern JS

Introduction

Sometimes map() returns an array for each element—splitting words, duplicating values, or expanding records. You then need to flatten those nested arrays. flatMap() does both in one readable call.

Think of it as map(callback).flat(1). The callback can return a single value or an array; arrays are merged one level into the final result.

Understanding the flatMap() Method

For each element, flatMap runs your callback. If the callback returns [a, b], those items are appended to the output. If it returns a non-array value, that value is appended as one item.

Unlike flat(), you cannot choose depth—flattening is always exactly one level. Deeper nesting in the callback result stays nested unless you flatten again manually.

💡
Beginner Tip

Return [] to skip an element (filter) and [item] to keep one. This replaces filter().map() in many cases.

📝 Syntax

General form of Array.prototype.flatMap:

JavaScript
array.flatMap(callback(element, index, array), thisArg)

Parameters

  • callback — called for each element; return a value or array to include in the result.
  • thisArg — optional this value inside the callback.

Return value

  • A new array with mapped values flattened one level.
  • Original array unchanged.
  • Sparse holes are skipped (unlike map().flat() in some cases).

Common patterns

  • arr.flatMap((x) => [x * 2]) — map to single-item arrays.
  • arr.flatMap((s) => s.split(" ")) — split and flatten words.
  • arr.flatMap((n) => (n % 2 === 0 ? [n] : [])) — filter evens.
  • arr.flatMap((x) => x.tags) — extract nested arrays from objects.

⚡ Quick Reference

GoalCode
Map and flatten one levelarr.flatMap(fn)
Equivalent toarr.map(fn).flat(1)
Filter + map in one passflatMap(x => cond ? [x] : [])
Mutates original?No
Flatten depthAlways 1

📋 flatMap() vs map() vs flat() vs map().flat()

Use flatMap when each element becomes zero or more output items in one pass.

flatMap
map + flat(1)

Transform & merge

map
1:1 transform

Keeps nesting

flat
flatten only

No callback

map().flat()
two steps

Older pattern

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Basic map-and-flatten behavior.

Example 1 — Map Each Number to a Single Value

Returning [num * 2] from the callback maps and flattens in one step.

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

const doubled = numbers.flatMap((num) => [num * 2]);

console.log(doubled);
// [2, 4, 6, 8]
Try It Yourself

How It Works

Each callback returns a one-element array. flatMap merges them into a flat list of doubled values.

Example 2 — Split Words into Characters

A classic use case: transform each string into an array of characters, then flatten.

JavaScript
const words = ["Hello", "World"];

const chars = words.flatMap((word) => word.split(""));

console.log(chars);
// ["H", "e", "l", "l", "o", "W", "o", "r", "l", "d"]
Try It Yourself

How It Works

split("") returns an array per word. Without flattening you would get nested arrays; flatMap merges them automatically.

📈 Practical Patterns

Filter, compare, and extract nested data.

Example 3 — Filter with Empty Arrays

Return [num] to keep an even number, or [] to drop it—filter and map in one pass.

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

const evens = numbers.flatMap((num) =>
  num % 2 === 0 ? [num] : []
);

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

How It Works

Empty arrays add nothing to the output. This pattern also skips sparse holes in the source array, which is useful with sparse data.

Example 4 — flatMap() vs map()

See why map() leaves nested arrays while flatMap() does not.

JavaScript
const ids = [10, 20];

const withMap = ids.map((id) => [id, id + 1]);
const withFlatMap = ids.flatMap((id) => [id, id + 1]);

console.log("map:", withMap);
// [[10, 11], [20, 21]]

console.log("flatMap:", withFlatMap);
// [10, 11, 20, 21]
Try It Yourself

How It Works

When your callback returns arrays, choose flatMap if you want a single merged list. Use map when nested structure is intentional.

Example 5 — Extract Tags from Records

Pull tag arrays from objects and flatten into one list of all tags.

JavaScript
const posts = [
  { title: "Intro", tags: ["js", "tutorial"] },
  { title: "Arrays", tags: ["flatMap", "map"] }
];

const allTags = posts.flatMap((post) => post.tags);

console.log(allTags);
// ["js", "tutorial", "flatMap", "map"]
Try It Yourself

How It Works

Each post contributes its tags array. flatMap concatenates them without a separate flat() call.

🚀 Common Use Cases

  • Tokenizing text — split sentences or words into flat character or word lists.
  • Filter + map — one pass with [] / [value] returns.
  • Collecting nested fields — gather tags, IDs, or child items from records.
  • Expanding ranges — each input produces multiple output values.
  • Removing optional items — return empty array when data is missing.
  • Replacing map().flat() — cleaner pipelines in modern code.

🧠 How flatMap() Runs

1

Loop elements

Skip holes in sparse arrays; call callback for each value.

Iterate
2

Map via callback

Callback returns a value or array per element.

Transform
3

Flatten one level

Array results merge; non-arrays append as single items.

Merge
4

Return new array

Source array unchanged; output is one level flatter.

📝 Notes

  • Always flattens exactly one level—no depth parameter.
  • Callback may return a non-array; it becomes one output element.
  • Return [] to omit an element (filter pattern).
  • Sparse arrays: holes are skipped, unlike some map().flat() cases.
  • Not a replacement for flat(Infinity) on deeply nested existing arrays.
  • ES2019—polyfill or use map().flat() for IE11.

Browser & Runtime Support

Array.prototype.flatMap() was added in ES2019 alongside flat(). It is available in all evergreen browsers and modern Node.js versions.

Baseline · ES2019

Array.prototype.flatMap()

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

96% 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. For IE11, use map().flat() or a polyfill.

Conclusion

flatMap() is the go-to method when each input element produces zero or more outputs and you want a single flat array. It replaces the common map().flat() pattern with clearer, shorter code.

Next, explore forEach() for side-effect loops that do not build a new array.

💡 Best Practices

✅ Do

  • Use flatMap when callbacks return arrays
  • Filter with [] and [value] when it reads well
  • Prefer it over map().flat() in new code
  • Return arrays consistently when flattening matters
  • Feature-detect for legacy browsers if needed

❌ Don’t

  • Expect multi-level flattening—only depth 1
  • Use it when map() alone is enough
  • Forget that non-array returns stay as single items
  • Chain flatMap when one callback can do the work
  • Rely on it in IE11 without a fallback

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.flatMap()

Map and flatten in one expressive method call.

5
Core concepts
📈 02

map + flat

One step.

Pipeline
📐 03

Depth 1

Always one level.

Limit
🗃 04

Filter trick

[] or [x].

Pattern
📦 05

New array

Immutable.

Safe

❓ Frequently Asked Questions

flatMap() runs map() on each element, then flattens the result by exactly one level. It is shorthand for 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.
Did you know?

flatMap is slightly stricter than map().flat() about sparse arrays: it does not call your callback for empty slots and does not include them in the result. For dense arrays the outputs match.

Continue to forEach()

Learn how to run a function on each array element for side effects without building a new array.

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.

6 people found this page helpful