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 Array.prototype.includes Returning False for NaN Because a Custom Equality Check Replaced Its Built-In SameValueZero Comparison

- 05.11.26 - ErcanOPAK

⚡ JavaScript: Fix Array.prototype.includes Returning False for NaN Because a Custom Equality Check Replaced Its Built-In SameValueZero Comparison

Array.prototype.includes is specified to use the SameValueZero algorithm, which is the one built-in piece of JavaScript that actually treats NaN as equal to itself, unlike ===, so [NaN].includes(NaN) correctly returns true with no extra work. Code that replaces a plain .includes() call with a manual .some(x => x === value) check — to add logging, a transform, or a custom comparator — loses that behavior silently, because === never considers NaN equal to anything, including another NaN, so the exact same lookup that worked with .includes() starts returning false the moment it’s rewritten with strict equality.

🔎 The Problem

const seen = [1, 2, NaN, 4];

seen.includes(NaN);
// true - Array.prototype.includes uses SameValueZero

seen.some(x => x === NaN);
// false - === never considers NaN equal to anything, even NaN

// Rewriting a lookup from .includes() to a manual .some() check
// silently loses NaN handling, with no error anywhere.

✅ Fix: Use Object.is When You Reimplement the Check Yourself

  • If you’re replacing .includes() with a manual search for any reason, use Object.is(a, b) instead of === in the comparator to keep the same NaN-equals-NaN behavior .includes() gave you for free.
  • Add a unit test with NaN specifically present in the collection whenever a lookup function is rewritten from a built-in array method to a custom one, since this is exactly the kind of edge case a normal test suite built around real-world values never happens to exercise.
  • If the collection can plausibly contain NaN — parsed numeric input is the most common source — keep using .includes() directly rather than reimplementing the search, since there’s rarely a good reason to give up its NaN handling on purpose.

🚨 Why This Is Dangerous

  • A “does this collection contain this value” check that silently stops recognizing NaN as present can let duplicate or invalid NaN entries slip past a dedup or validation step that used to catch them, with no error anywhere, because the code still runs — it just quietly returns the wrong answer for one specific value.

NaN breaks === before it breaks anything else in your code — .includes() is one of the rare spots where the language already fixed that for you, right up until you rewrite it yourself.

— JavaScript Tip

Related posts:

AJAX Cache Nightmare — Why Your GET Requests Don’t Update

AJAX Cache Nightmare — Why Your GET Requests Don’t Update

Event Listeners Causing Memory Leaks

Event Listeners Causing Memory Leaks

JavaScript: Fix a ResizeObserver Callback Firing in an Infinite Loop Because It Resizes the Very Ele...

JavaScript: Fix a ResizeObserver Callback Firing in an Infinite Loop Because It Resizes the Very Ele...

Views: 8

Post navigation

Git: Fix git rerere Failing to Replay a Recorded Resolution Because the Conflict Markers Shifted Slightly on the Next Rebase
WordPress: Fix a Multisite Network’s Shared Media Library Returning 403 for Subsites Because the Uploads Path Rewrite Only Covers the Main Site’s Domain

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 (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)
  • 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 @ (607)
  • Average of all values in a column that are not zero in SQL (569)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • 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 (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)

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