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
| Parameter | Description |
|---|---|
depth optional |
How many levels to flatten; Infinity flattens everything. Default: 1 |
Return value
A new array.
Examples
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 ]
console.log([1, , 3].flat()); // also removes holes
Output
[ 1, 3 ]
Version history
| Version | Change |
|---|---|
| JavaScript ES2019 | Added |
Related
array.flatMap()· Map each element with a function, then flatten the result one level. Useful when one element becomes several.array.concat()· Return a new array that joins this array with other arrays or values. The original arrays are not changed.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial