---
title: "Comment by Purvi Barot on \"Array.flat()\" | Hashnode"
description: "ES2019 introduced the Array.prototype.flat() method that creates a new array with all the elements of the subarrays concatenated to it recursively up to a speci"
url: "https://hashnode.com/posts/arrayflat/5feb6a27b2cf2458fe323505/comment/5ff7fc7daa5c467fe15570ef"
---

[Discussion](https://hashnode.com/posts/arrayflat/5feb6a27b2cf2458fe323505)

Comment [Array.flat()](https://hashnode.com/posts/arrayflat/5feb6a27b2cf2458fe323505)

[Purvi Barot](https://hashnode.com/@purvi)

[Jan 8, 2021](https://hashnode.com/posts/arrayflat/5feb6a27b2cf2458fe323505/comment/5ff7fc7daa5c467fe15570ef)

ES2019 introduced the Array.prototype.flat() method that creates a new array with all the elements of the subarrays concatenated to it recursively up to a specified depth.

The following shows the syntax of the flat() method:

```
let newArray = arrayObject.flat([depth])
```

```
let arr = [[1, 2],[3, 4, [5, 6 [7, 8], 9]],[10 [11, 12]]];

// since the default depth is 1
// only the first level arrays in the parent array are flattend
// you would notice that there's a second level nesting after the number '4', in the parent array

arr.flat();
// [ 1, 2, 3, 4, [ 5, 6, [ 7, 8 ], 9 ], 10, [ 11, 12 ] ]


// we supplied 2 as the depth

arr.flat(2);
// [ 1, 2, 3, 4, 5, 6, [ 7, 8 ], 9, 10, 11, 12 ]


// using 'Infinty' as an argument means flatten all levels to a one-dimensional array

arr.flat(Infinity);
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]

// the flat() method also remove empty slots in the array, otherwise called 'array holes'
let arrrayHole = [1, 2, , 4, 5];
arrayHole.flat();
// [1, 2, 4, 5]
```
