Skip to content

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 Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

- 04.10.26 - ErcanOPAK

⚡ The Default Value That Only Worked for One Kind of Missing

A default value in a destructuring pattern only kicks in when the corresponding position is exactly undefined – not when it is missing from a shorter array, and specifically not when it is present but holds the value null, which JavaScript treats as a deliberate, distinct value rather than an absence of one. Code that destructures the result of a function which can return null for a field – a database lookup that found no match, or a parsed value that failed to parse – quietly skips every default value for that field, leaving null to flow forward into code that expected either a real value or the configured fallback, and getting neither.

🔎 The Problem

function getUserPrefs(userId) {
    // theme comes back as null specifically when the user has no
    // saved preference row at all - not undefined, null:
    const row = lookupPrefsRow(userId);
    return [row ? row.theme : null, row ? row.fontSize : null];
}

const [theme = 'light', fontSize = 14] = getUserPrefs(42);

// Expected: theme === 'light', fontSize === 14, when there's no
// saved row. What actually happens: theme === null, fontSize ===
// null - the default values never apply, because null is not
// undefined, and a destructuring default only triggers for
// undefined specifically.

applyTheme(theme);      // called with null, not 'light'
setFontSize(fontSize);  // called with null, not 14

✅ Fix: Normalize to Undefined Before the Default Ever Has to Apply

  • Convert a deliberate null into undefined at the source – right where the lookup can return null – before the value ever reaches a destructuring pattern, so the default value mechanism sees exactly the signal it actually checks for.
  • Use the nullish coalescing operator explicitly on each value instead of relying on a destructuring default when a value can be either null or undefined, since nullish coalescing treats both the same way and a destructuring default does not.
  • Add a direct equality check for null immediately after any API, parser, or database call known to return it for a missing value, and decide there – deliberately – whether the rest of the code should treat that null the same as undefined or differently.

⚠️ Why This Is Easy to Miss

  • A destructuring default reads as a general catch-all for any kind of missing value, so it is a reasonable, common assumption that it covers null as well as undefined – the actual rule is narrower than the syntax visually suggests.
  • A quick manual test calling the function with a completely missing argument, rather than one that explicitly returns null, exercises the undefined case and shows the default working correctly, hiding the gap until a real null value happens to flow through.

A destructuring default only answers to one specific value – null raises its hand just as clearly as any value does, and the default never hears it.

— JavaScript Tip

Related posts:

JavaScript: Working with Promises and Async/Await

JavaScript: Why a Set Still Contains 'Duplicate' Values That Look Completely Identical

JavaScript Objects Grow Without You Noticing

Post Views: 0

Post navigation

Kubernetes: Fix a Sidecar Container That Keeps the Pod Running Forever Because It Never Signals Completion to the Job Controller
ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In

Leave a Reply Cancel reply

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

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

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

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (947)
  • How to add default value for Entity Framework migrations for DateTime and Bool (938)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (836)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

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