Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. JavaScript
  4. flat() and flatMap() in JavaScript

flat() and flatMap() in JavaScript

Date- May 19,2023 Updated Mar 2026 4002
javascript array methods

Understanding flat()

The flat() method creates a new array by concatenating all subarrays within a nested array up to a specified depth. This allows developers to easily handle complex data structures without the need for cumbersome loops. The method can be particularly useful when dealing with APIs that return nested data, such as JSON responses.

When using flat(), you can specify the depth of flattening. If no depth is specified, the default is 1, which means only the first level of subarrays will be flattened. This feature provides flexibility in how you manipulate your arrays.

const nestedArray = [1, [2, 3], [4, [5, 6]]];
const flattenedArray = nestedArray.flat();
console.log(flattenedArray); // Output: [1, 2, 3, 4, [5, 6]]

const deeplyNestedArray = [1, [2, [3, [4]]]];
const deeplyFlattenedArray = deeplyNestedArray.flat(2);
console.log(deeplyFlattenedArray); // Output: [1, 2, 3, [4]]

In the example above, flat() is called on the nestedArray with no depth specified, resulting in a new array where subarrays are concatenated up to a depth of 1. The nested subarray [5, 6] remains intact because the depth is 1. In contrast, when called with a depth of 2 on deeplyNestedArray, it flattens the array further.

Exploring flatMap()

The flatMap() method combines the functionality of map() and flat() in a single step. This means that it not only transforms the elements in an array but also flattens the result into a new array, making it incredibly useful for handling transformations that yield arrays as outputs.

When using flatMap(), the callback function is applied to each element of the array, and the return value of this function is flattened into the new array. This method is particularly useful in scenarios like data transformation, where each element might need to be converted into multiple elements.

const numbers = [1, 2, 3, 4, 5];
const doubledArray = numbers.flatMap(num => [num, num * 2]);
console.log(doubledArray); // Output: [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]

const sentences = ['Hello', 'World'];
const chars = sentences.flatMap(sentence => sentence.split(''));
console.log(chars); // Output: ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']

In the first example, flatMap() is used to create a new array where each element of the original array is mapped to two elements: the original value and its double. The second example demonstrates how sentences can be split into individual characters, showcasing the dual functionality of mapping and flattening.

Edge Cases & Gotchas

While flat() and flatMap() are powerful methods, there are some edge cases and gotchas to be aware of. One important aspect is that both methods only flatten arrays; if you attempt to flatten an object or an unsupported type, it will not work as expected.

Another potential issue arises with sparse arrays. When using flat(), undefined values in the array may lead to unexpected results. Similarly, using flatMap() with a callback that returns undefined can result in those entries being omitted in the final output.

const sparseArray = [1, , 2, [3, , 4]];
const flattenedSparse = sparseArray.flat();
console.log(flattenedSparse); // Output: [1, 2, 3, , 4]

const undefinedExample = [1, 2, 3];
const flatMappedUndefined = undefinedExample.flatMap(num => { if (num > 1) return num; });
console.log(flatMappedUndefined); // Output: [2, 3]

In the first example, the sparse array retains its empty slots even after flattening. The second example shows how using a condition in the mapping function can lead to omitted values in the final array.

Performance & Best Practices

When considering performance, it is essential to understand that both flat() and flatMap() are generally efficient for moderate-sized arrays. However, with deeply nested structures or very large arrays, performance can degrade. It is advisable to test and profile your code to ensure that it meets performance requirements.

Best practices for using these methods include:

  • Use flat() when you need to flatten an array without modifying the original structure.
  • Utilize flatMap() when you need to transform data into a new structure while flattening it in one go.
  • Be cautious of the depth parameter in flat() to avoid unintentionally losing data.
  • Always ensure your callback functions in flatMap() return consistent types to avoid unexpected results.

Conclusion

In summary, the flat() and flatMap() methods introduced in ES10 are invaluable tools for developers working with nested arrays. They simplify array manipulation and can lead to cleaner, more maintainable code. Here are the key takeaways:

  • flat() flattens nested arrays up to a specified depth.
  • flatMap() combines mapping and flattening into a single operation.
  • Both methods are non-mutating, preserving the original array.
  • Be mindful of edge cases such as sparse arrays and undefined values.
  • Consider performance implications with large or deeply nested arrays.
flat() and flatMap()

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

How to shuffle an array using javascript
May 07, 2022
Mastering Navigation in React with React Router
Apr 02, 2026
Mastering React: A Comprehensive Guide to Getting Started
Apr 02, 2026
Mastering JavaScript Error Handling with Try, Catch, and Finally
Mar 31, 2026
Previous in JavaScript
Get User Location using JS
Next in JavaScript
How to get visitors location like country and city
Buy me a pizza

Comments

On this page

๐ŸŽฏ

Interview Prep

Ace your JavaScript interview with curated Q&As for all levels.

View JavaScript Interview Q&As

More in JavaScript

  • Complete Guide to Slick Slider in JavaScript with Examples 14923 views
  • Card Number Formatting using jquery 11609 views
  • Alphanumeric validation in JavaScript 8823 views
  • Jquery Autocomplete 8435 views
  • Input Mask in Jquery 7522 views
View all JavaScript posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor