JavaScript (ES2026) reference · Array Methods

JavaScript Array flatMap() Method

Map each element with a function, then flatten the result one level. Useful when one element becomes several.

JavaScript (ES2026) Since ES2019

Syntax

array.flatMap(callbackFn, thisArg)

Parameters

ParameterDescription
callbackFn Called for each element with (element, index, array).

Return value

A new array.

Examples

Split into wordsTry it yourself
const sentences = ["hello world", "good morning"];
console.log(sentences.flatMap((s) => s.split(" ")));

Output

[ 'hello', 'world', 'good', 'morning' ]
Filter and duplicate in one passTry it yourself
const nums = [1, 2, 3, 4];
console.log(nums.flatMap((n) => (n % 2 ? [n, n] : [])));

Output

[ 1, 1, 3, 3 ]

Version history

VersionChange
JavaScript ES2019Added