Favorites

JavaScript Array.prototype.flat() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Array.prototype.flat()
JavaScript Array Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

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 Infinity to flatten completely.

Return Value

A new array with the sub-array elements joined into it.

Example

const nested = [1, [2, 3], [4, [5, [6]]]];
console.log(nested.flat());
console.log(nested.flat(2));
console.log(nested.flat(Infinity));

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee