Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
JavaScript

JavaScript: Fix an Array.flat() Call That Silently Stops at One Level Because the Default Depth Isn’t Infinity

- 10.10.26 - ErcanOPAK

⚡ The Flatten That Only Went One Floor Down

Array.prototype.flat() takes an optional depth argument controlling how many levels of nested arrays it flattens, and it’s easy to assume calling it with no argument at all flattens everything, the way a method simply named ‘flat’ sounds like it should. The actual default depth is 1, not Infinity – a single call only flattens one level of nesting, leaving any array nested two or more levels deep still wrapped in its own array inside the result. Data that happens to be nested exactly one level deep flattens correctly and the code ships looking right, right up until a source that produces two or three levels of nesting runs through the same call and silently leaves nested arrays sitting inside what was supposed to be a flat list.

🔎 The Problem

const groups = [[1, 2], [3, [4, 5]], [6]];

// Looks like it should fully flatten - the method is just called
// "flat", after all. The default depth argument is 1, not Infinity.
const result = groups.flat();
console.log(result);
// [1, 2, 3, [4, 5], 6]
// The [4, 5] sub-array is still nested - it was two levels deep,
// and only one level of flattening happened.

// Code downstream expects a flat array of numbers and silently
// gets a mixed array containing a nested array instead - no error,
// just wrong data shape further down the pipeline.
const total = result.reduce((sum, n) => sum + n, 0);
// NaN - adding a number to an array produces NaN, with no
// exception pointing back at the actual root cause.

✅ Fix: Be Explicit About the Depth You Actually Need

  • Pass Infinity explicitly – array.flat(Infinity) – whenever the intent is to fully flatten arbitrarily nested data, rather than relying on the default, which only handles exactly one level.
  • If the maximum nesting depth is known and fixed, pass that exact number instead of guessing, making the intended depth visible in the code rather than implicit in whatever the default happens to be.
  • Add a test case using data nested more than one level deep specifically, since data nested exactly one level will pass with the default and never reveal that deeper nesting isn’t handled.

⚠️ Why This Is Easy to Miss

  • The method’s name alone – flat() – reads as a complete, no-argument guarantee of a fully flat result, which is a reasonable assumption that happens to be wrong about exactly this one default value.
  • Works correctly for any data source that happens to nest exactly one level deep, so the bug only appears once a different or evolving data source starts producing two or more levels of nesting that the original testing never exercised.

flat() with no argument doesn’t mean ‘fully flatten’ – it means ‘flatten exactly one level’, and the method’s own name is the only thing suggesting otherwise.

— JavaScript Tip

Related posts:

JavaScript: Use Memoization for Performance

JavaScript: Use Memoization for Performance

JavaScript: Why a Forgotten clearInterval() Is Silently Draining Your Users' Battery

JavaScript: Why a Forgotten clearInterval() Is Silently Draining Your Users' Battery

JavaScript: Fix a MutationObserver Callback That Silently Stops Firing After the Observed Node Is Re...

JavaScript: Fix a MutationObserver Callback That Silently Stops Firing After the Observed Node Is Re...

Views: 6

Post navigation

CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
Visual Studio: Fix IntelliSense That Shows APIs From the Wrong Target Framework in a Multi-Targeted Project

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com