Standard built-ins

Array.prototype.flat()

Try the free JavaScript compiler

Flattens actual nested arrays up to depth, default 1. Skips ordinary holes at levels traversed. Nonarray values remain as values, and the operation is not a recursive clone of arbitrary objects.

Formal Syntax & Structure

javascript syntaxjavascript
array.flat(depth?)

Parameters & Attributes

NameTypeDescription
depthnumberOptional nonnegative nesting depth after conversion and clamping; Infinity flattens finite nesting fully.

Return Value

Array

Flattened result.

Working Example

Example Snippetjavascript
console.log(JSON.stringify([1,[2,[3]]].flat(2)));
JavaScript Reference