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
Fundamentals
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.
Concept
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.
Each post contributes its tags array. flatMap concatenates them without a separate flat() call.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.flatMap()Excellent
Bottom line: Safe for modern web apps. For IE11, use map().flat() or a polyfill.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.flatMap()
Map and flatten in one expressive method call.
5
Core concepts
📝01
Syntax
arr.flatMap(fn)
API
📈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.