Javascript flatten array and js flatten array usually mean turning nested arrays into a single list of values. In modern JavaScript, Array.prototype.flat (ES2019) is the standard array flat javascript tool: it returns a new array and only unwraps elements that are Array instances. For map plus one-level flatten, use flatMap. Older stacks may still use recursion with concat or reduce—the same idea as how to flatten an array in javascript tutorials before flat existed. Nested literals often appear after concat or spread merges; see add to an array when you are building lists to flatten later.
Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.
Quick reference
flat / flatMap return new arrays—choose depth, Infinity, or a legacy reducer based on engine support and how deep nesting can grow.
| Need | Use |
|---|---|
| One level | arr.flat() or arr.flat(1) |
| All levels | arr.flat(Infinity) |
| Map + flatten 1 level | arr.flatMap(fn) |
| Legacy / no ES2019 | Recursive concat / reduce |
flat() — default depth 1 (flatten array js)
Default depth is 1: only the outermost nested arrays are opened.
const nestedArray = [1, [2, [3, 4], 5], 6];
console.log(nestedArray.flat());You should see one line like: [ 1, 2, [ 3, 4 ], 5, 6 ].
flat(Infinity) — fully flatten (how to flatten an array in javascript)
Infinity means keep flattening until no nested Array values remain.
const nestedArray = [1, [2, [3, 4], 5], 6];
console.log(nestedArray.flat(Infinity));You should see one line like: [ 1, 2, 3, 4, 5, 6 ].
flat(depth) — specific depth (js array flatten control)
Pass a numeric depth to flatten exactly that many levels.
console.log([1, [2, [3, [4]]]].flat(2));You should see one line like: [ 1, 2, 3, [ 4 ] ].
Two levels are flattened; one nested [4] remains.
flatMap — map then flatten one level (flat array js)
Equivalent to map(...).flat(1), but in one method:
console.log([1, 2, 3].flatMap((x) => [x, x * 2]));You should see one line like: [ 1, 2, 2, 4, 3, 6 ].
Recursive concat loop (flattening an array without flat)
Useful where flat is unavailable; watch stack depth on extremely deep trees. Legacy helpers branch on Array.isArray rather than instanceof so cross-realm arrays still flatten—the JavaScript instanceof guide explains when constructor checks lie.
function flattenArray(arr) {
let flattened = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
flattened = flattened.concat(flattenArray(arr[i]));
} else {
flattened.push(arr[i]);
}
}
return flattened;
}
const nestedArray = [1, [2, [3, 4], 5], 6];
console.log(flattenArray(nestedArray));You should see one line like: [ 1, 2, 3, 4, 5, 6 ].
Recursive reduce + concat
Another recursive style; prefer flat(Infinity) when the runtime supports it.
function flattenArray(arr) {
return arr.reduce(
(acc, val) =>
Array.isArray(val) ? acc.concat(flattenArray(val)) : acc.concat(val),
[]
);
}
const nestedArray = [1, [2, [3, 4], 5], 6];
console.log(flattenArray(nestedArray));You should see one line like: [ 1, 2, 3, 4, 5, 6 ].
Non-array elements and sparse arrays
flat does not unwrap arbitrary objects—only Array instances. It also does not deep-clone nested objects inside arrays; inner references stay shared like any shallow copy in the JavaScript copy array guide:
console.log([1, 2, { a: 3 }].flat());You should see one line like: [ 1, 2, { a: 3 } ].
Sparse roots interact with flat in subtle ways; one illustration:
console.log([1, , [3, , 4]].flat());You should see one line like: [ 1, 3, 4 ].
If you must preserve holes, avoid pipelines that mix flat with sparse data unless you normalize first.
Summary
Modern code starts with flat / flatMap; keep recursive concat in your pocket for pre-ES2019 environments and remember only real Array elements unwrap.
flat()defaults to depth 1; useflat(Infinity)for arbitrary nesting.flatMapismapplus one-level flatten in one pass.- Recursive
concat/reducepatterns cover legacy environments withoutflat. - Only real arrays flatten; plain objects and sparse holes need explicit handling.
References
MDN references for flat, flatMap, and classic helpers used in flatten array js recipes.
- MDN:
Array.prototype.flat() - MDN:
Array.prototype.flatMap() - MDN:
Array.prototype.concat() - MDN:
Array.prototype.reduce()
Frequently Asked Questions
Does flat mutate the original array for javascript flatten array code?
No. flat and flatMap return a new array (a shallow copy of structure). The original nested arrays are left unchanged.
What is the default depth of Array.prototype.flat?
- Only one level of nested arrays is unwrapped unless you pass a higher number or Infinity.
When should I use flatMap instead of map then flat?
When you want map followed by a one-level flatten. flatMap is equivalent to map then flat(1) and is slightly more efficient and clearer.
How do I flatten an array in javascript of unknown nesting depth?
Use nestedArray.flat(Infinity) in modern engines. In very old environments without flat, use a recursive helper that concatenates arrays.
Does flat unwrap plain objects that look like arrays?
No. flat only flattens elements that are arrays. Other values, including plain objects, are copied into the result as-is.
What happens to empty slots in a sparse array when I call flat?
flat can remove some empty slots depending on depth and structure; always inspect results if you rely on holes. Prefer dense arrays when possible.
