Array

Array.prototype.flat

Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth

Syntax

JavaScript
array.flat(depth?)

Parameters

ParameterTypeDescription
depthnumberDepth level specifying how deep a nested array structure should be flattened. Defaults to 1

Return Value

A new array with the sub-array elements concatenated

Examples

Basic Usage
const nested = [1, [2, 3], [4, [5, 6]]];
console.log(nested.flat()); // [1, 2, 3, 4, [5, 6]]
Practical Example
const deep = [1, [2, [3, [4]]]];
console.log(deep.flat(Infinity)); // [1, 2, 3, 4]
Advanced Usage
const withHoles = [1, , 3, , 5];
console.log(withHoles.flat()); // [1, 3, 5]

Understanding Array.prototype.flat

The Array.prototype.flat method in JavaScript creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.

The method signature is array.flat(depth?). It accepts 1 parameter: depth. When called, it returns a new array with the sub-array elements concatenated. Understanding when and how to use flat() helps you write more expressive, readable code.

Common use cases for Array.prototype.flat include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-flatmap, array-concat, array-map, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Array.prototype.flat is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Array Methods

Other methods in the Array object

Related Tools

More Array Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.