• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • Array.prototype.flat()
      • Deutsch
      • Español
      • Français
      • 日本語
      • 한국어
      • Português (do Brasil)
      • Русский
      • 中文 (简体)
      • 正體中文 (繁體)

    In this article

    • Try it
    • Syntax
    • Description
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. Array.from()
    2. Array[Symbol.species]
  • Instance methods
    1. Array.prototype.entries()
    2. Array.prototype.find()
    3. Array.prototype.flat()
    4. Array.prototype.indexOf()
    5. Array.prototype.map()
    6. Array.prototype.reduceRight()
    7. Array.prototype.some()
    8. Array.prototype.toReversed()
    9. Array.prototype.unshift()
    10. Array: length
    11. Function.prototype.bind()
    12. Function: displayName Non-standard
    13. Function.prototype.arguments Non-standard Deprecated
    14. Function.prototype.caller Non-standard Deprecated
  • Instance methods
    1. Object.prototype.__defineGetter__() Deprecated
    2. Object.prototype.__defineSetter__() Deprecated
    3. Object.prototype.__lookupGetter__() Deprecated
    4. Object.prototype.__lookupSetter__() Deprecated
    5. Object.prototype.toLocaleString()
    6. Object.prototype.__proto__ Deprecated
    7. Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

  • Try it

    const arr1 = [0, 1, 2, [3, 4]];
    
    console.log(arr1.flat());
    / expected output: Array [0, 1, 2, 3, 4]
    
    const arr2 = [0, 1, [2, [3, [4, 5]]]];
    
    console.log(arr2.flat());
    / expected output: Array [0, 1, 2, Array [3, Array [4, 5]]]
    
    console.log(arr2.flat(2));
    / expected output: Array [0, 1, 2, 3, Array [4, 5]]
    
    console.log(arr2.flat(Infinity));
    / expected output: Array [0, 1, 2, 3, 4, 5]
    

    Syntax

    js
    flat()
    flat(depth)
    

    Parameters

    depth Optional

    The depth 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 into it.

    Description

    The flat() method is a shallow copy that contains the same elements as the ones from the original array.

    The flat() method removes empty slots if the array being flattened is sparse. For example, if depth is 1, both empty slots in the root array and in the first level of nested arrays are ignored, but empty slots in further nested arrays are preserved with the arrays themselves.

    The flat() method is generic. It only expects the this value to have a length property and integer-keyed properties. However, its elements must be arrays if they are to be flattened.

    Examples

    Flattening nested arrays

    js
    const arr1 = [1, 2, [3, 4]];
    arr1.flat();
    / [1, 2, 3, 4]
    
    const arr2 = [1, 2, [3, 4, [5, 6]]];
    arr2.flat();
    / [1, 2, 3, 4, [5, 6]]
    
    const arr3 = [1, 2, [3, 4, [5, 6]]];
    arr3.flat(2);
    / [1, 2, 3, 4, 5, 6]
    
    const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
    arr4.flat(Infinity);
    / [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    

    Using flat() on sparse arrays

    The flat() method removes empty slots in arrays:

    js
    const arr5 = [1, 2, , 4, 5];
    console.log(arr5.flat()); / [1, 2, 4, 5]
    
    const array = [1, , 3, ["a", , "c"]];
    console.log(array.flat()); / [ 1, 3, "a", "c" ]
    
    const array2 = [1, , 3, undefined, ["a", , ["d", , "e"]], null];
    console.log(array2.flat()); / [ 1, 3, undefined, "a", ["d", empty, "e"], null ]
    console.log(array2.flat(2)); / [ 1, 3, undefined, "a", "d", "e", null ]
    

    Calling flat() on non-array objects

    The flat() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length. If the element is not an array, it's directly appended to the result. If the element is an array, it's flattened according to the depth parameter.

    js
    const arrayLike = {
      length: 3,
      0: [1, 2],
      / Array-like objects aren't flattened
      1: { length: 2, 0: 3, 1: 4 },
      2: 5,
      3: 3, / ignored by flat() since length is 3
    };
    console.log(Array.prototype.flat.call(arrayLike));
    / [ 1, 2, { '0': 3, '1': 4, length: 2 }, 5 ]
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-array.prototype.flat

    Browser compatibility

    See also

    • Polyfill of Array.prototype.flat in core-js
    • es-shims polyfill of Array.prototype.flat
    • Indexed collections guide
    • Array
    • Array.prototype.concat()
    • Array.prototype.flatMap()
    • Array.prototype.map()
    • Array.prototype.reduce()

    Help improve MDN

    flat() and flatMap() methods for arrays creates a new array such that each nested array item is concatenated into it.","name":"Array flat() and flatMap()"}},"browserCompat":["javascript.builtins.Array.flat"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant