JavaScript (ES2026) reference · Array Methods

JavaScript Array flat() Method

Return a new array with nested arrays flattened into it, one level deep by default or to a given depth.

JavaScript (ES2026) Since ES2019

Syntax

array.flat(depth)

Parameters

ParameterDescription
depth optional How many levels to flatten; Infinity flattens everything. Default: 1

Return value

A new array.

Examples

Flatten nested arraysTry it yourself
const nested = [1, [2, [3, [4]]]];
console.log(nested.flat());
console.log(nested.flat(2));
console.log(nested.flat(Infinity));

Output

[ 1, 2, [ 3, [ 4 ] ] ]
[ 1, 2, 3, [ 4 ] ]
[ 1, 2, 3, 4 ]
Holes are removedTry it yourself
console.log([1, , 3].flat()); // also removes holes

Output

[ 1, 3 ]

Version history

VersionChange
JavaScript ES2019Added