---
title: "Discussion on \"Array.flat()\" | Hashnode"
description: "Discussion on \"Array.flat()\". const array = [1, [2, [3, 4, [5, 6]]], 7];\n\nconsole.log(array.flat(1)); // [ 1, 2, [ 3, 4, [ 5, 6 ] ], 7 ]\nconsole.log(array.flat(2)); // [ 1, 2, 3, 4, [ 5, 6 ], 7 ]\nconsole.log(array.flat(3)); // [ 1, 2, 3, 4, 5, 6, 7 ]\n\n.flat() (ES2019) will flatte..."
url: "https://hashnode.com/posts/arrayflat/5feb6a27b2cf2458fe323505"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Array.flat()](https://codedrops.hashnode.dev/arrayflat?utm_source=hashnode&utm_medium=feed)

const array = \[1, \[2, \[3, 4, \[5, 6\]\]\], 7\]; console.log(array.flat(1)); // \[ 1, 2, \[ 3, 4, \[ 5, 6 \] \], 7 \] console.log(array.flat(2)); // \[ 1, 2, 3, 4, \[ 5, 6 \], 7 \] console.log(array.flat(3)); // \[ 1, 2, 3, 4, 5, 6, 7 \] .flat() (ES2019) will flatte...

Dec 29, 2020 · 1 min read · codedrops.hashnode.dev  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#webdev](https://hashnode.com/tag/webdev)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#codenewbies](https://hashnode.com/tag/codenewbies)

## Written by

[Mehul Lakhanpal](https://hashnode.com/@ml318097)

@ml318097 · Bangalore, India

  
[View profile](https://hashnode.com/@ml318097)

Full-stack Developer

## Responses 2

Markdown supported · ⌘↵ to post

[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]
```

[Ashiru Olawale](https://hashnode.com/@walebant)

[Dec 29, 2020](https://hashnode.com/posts/arrayflat/5feb6a27b2cf2458fe323505/comment/5feb921d017ebb59232da158)

```
console.log(array.flat(Infinity));
```

will flat an array any level deep
