Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
The flatMap() method of Array instances returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map() followed by a flat() of depth 1 (arr.map(...args).flat()), but slightly more efficient than calling those two methods separately.
A function to execute for each element in the array. It should return an array containing new elements of the new array, or a single non-array value to be added to the new array. The function is called with the following arguments:
elementThe current element being processed in the array.
indexThe index of the current element being processed in the array.
arrayThe array flatMap() was called upon.
thisArg OptionalA value to use as this when executing callbackFn. See iterative methods.
A new array with each element being the result of the callback function and flattened by a depth of 1.
The flatMap() method is an iterative method. See Array.prototype.map() for a detailed description of the callback function. The flatMap() method is identical to map(callbackFn, thisArg) followed by flat(1) — for each element, it produces an array of new elements, and concatenates the resulting arrays together to form a new array. Read the iterative methods section for more information about how these methods work in general.
The flatMap() method is generic. It only expects the this value to have a length property and integer-keyed properties. However, the value returned from callbackFn must be an array if it is to be flattened.
Note that in this particular case the flatMap approach is slower than the for-loop approach — due to the creation of temporary arrays that must be garbage collected, as well as the return array not needing to be frequently resized. However, flatMap may still be the correct solution in cases where its flexibility and readability are desired.
While the above could have been achieved by using map itself, here is an example that better showcases the use of flatMap().
Let's generate a list of words from a list of sentences.
Notice, the output list length can be different from the input list length.
flatMap can be used as a way to add and remove items (modify the number of items) during a map. In other words, it allows you to map many items to many items (by handling each input item separately), rather than always one-to-one. In this sense, it works like the opposite of filter. Return a 1-element array to keep the item, a multiple-element array to add items, or a 0-element array to remove the item.
The array argument is useful if you want to access another element in the array, especially when you don't have an existing variable that refers to the array. The following example first uses filter() to extract operational stations and then uses flatMap() to create a new array where each element contains a station and its next station. On the last station, it returns an empty array to exclude it from the final array.
The array argument is not the array that is being built — there is no way to access the array being built from the callback function.
The callbackFn won't be called for empty slots in the source array because map() doesn't, while flat() ignores empty slots in the returned arrays.
The flatMap() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length. If the return value of the callback function is not an array, it is always directly appended to the result array.
| ECMAScript® 2027 Language Specification # sec-array.prototype.flatmap |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 20, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.