JavaScript Array.prototype.flat() Method
The flat() method creates a new array with nested arrays flattened into it, up to a given depth.
Description
The flat() method creates a new array with all sub-array elements joined into it, up to the given depth. By default it flattens one level, so [1, [2, [3]]].flat() gives [1, 2, [3]]. Pass Infinity to flatten every level.
It also removes empty slots from sparse arrays. The original array isn't changed.
flat() only flattens arrays; other array-like objects inside it are left as they are. For very deep nesting, flat(Infinity) is convenient but walks the whole structure, so prefer a specific depth when you know it. If you're mapping and then flattening one level, flatMap() combines both steps.
Syntax
array.flat()
array.flat(depth)Parameters
- depthOptional. How many levels of nesting to flatten. Defaults to 1; use
Infinityto flatten completely.
Return Value
A new array with the sub-array elements joined into it.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.flat() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





