← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

NaN — Detection & Avoiding Pitfalls

JavaScript ES2015 Beginner
debt(d3/e1/b1/t7)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). ESLint has built-in rules that flag isNaN() usage and suggest Number.isNaN() as the replacement. The detection_hints explicitly list eslint as automated detection, making this a default-tooling catch for the most common mistake.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: 'Replace isNaN() with Number.isNaN().' This is a direct, mechanical substitution requiring no refactoring or cross-file changes. After parseInt/parseFloat, adding a check is similarly a single-line guard.

b1 Burden Structural debt — long-term weight of choosing wrong

Closest to 'minimal commitment' (b1). NaN-checking is a localized validation concern. The choice to use Number.isNaN() over isNaN() imposes no structural debt—it's a local decision at the call site, affects only that validation point, and does not shape future architecture or require every maintainer to adopt a new pattern across the codebase.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The canonical misconception is that isNaN() and Number.isNaN() are equivalent; isNaN() coerces its argument (e.g., isNaN('hello') returns true), contradicting how strict equality and numeric operations work elsewhere in JavaScript. Developers transitioning from other languages or unfamiliar with JavaScript's type coercion rules will naturally assume isNaN() is the correct check. Additionally, comparing NaN === NaN always returns false, which is non-obvious and contradicts equality semantics in most contexts.

About DEBT scoring →

TL;DR

NaN is the only JavaScript value not equal to itself — use Number.isNaN() (not isNaN()) to detect it, as isNaN() coerces its argument first.

Explanation

NaN (Not a Number) is produced by invalid arithmetic (0/0, parseInt('abc'), Math.sqrt(-1)). Its defining property: NaN !== NaN (the only JS value not equal to itself). isNaN('abc') returns true because isNaN() coerces to number first — isNaN converts 'abc' to NaN then checks. Number.isNaN('abc') returns false (correct — 'abc' is a string, not NaN). Always use Number.isNaN() for reliable detection. For any value that may not be a valid number: typeof n === 'number' && !Number.isNaN(n).

Common Misconception

isNaN() and Number.isNaN() are equivalent — isNaN() coerces its argument, causing false positives for strings. Number.isNaN() is strict.

Why It Matters

NaN propagates silently through calculations — an undetected NaN can turn financial totals to NaN without throwing any error.

Common Mistakes

  • Using isNaN() which returns true for strings.
  • Not checking for NaN after parseInt/parseFloat on user input.
  • Comparing with === NaN which always returns false.

Code Examples

✗ Vulnerable
isNaN('hello')    // true — incorrect, 'hello' is a string, not NaN
isNaN(undefined)  // true — coercion trap
NaN === NaN        // false — cannot use ===
✓ Fixed
Number.isNaN(NaN)       // true
Number.isNaN('hello')   // false — correct
Number.isNaN(undefined) // false — correct

// Safe numeric check:
function isValidNumber(v) {
    return typeof v === 'number' && !Number.isNaN(v) && isFinite(v);
}

// After parsing:
const price = parseFloat(input);
if (Number.isNaN(price)) throw new Error('Invalid price');

Added 22 Mar 2026
Edited 5 Apr 2026
Views 103
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 2 pings W 0 pings T 1 ping F 0 pings S 0 pings S 2 pings M 0 pings T
No pings yet today
SEMrush 2
ChatGPT 9 Amazonbot 8 Google 7 SEMrush 7 Ahrefs 6 PetalBot 6 Unknown AI 4 Perplexity 4 Majestic 2 Scrapy 2 Brave Search 2 Applebot 2 Meta AI 1 Twitter/X 1 Bing 1 Sogou 1
crawler 57 crawler_json 4 pre-tracking 2
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
JavaScript javascript The programming language of the browser — it reads and modifies the page (the DOM), reacts to user events, and fetches data without reloading.

JavaScript is the only language browsers execute, so every interactive behaviour on the web goes through it. Its two defining traits — single-threaded event loop and loose typing (== coercion) — explain the majority of both its bugs and its design patterns.

💡 Default to const, use === always, and reach for let only when a value genuinely reassigns.

Ask Codex about JavaScript →
NaN javascript NaN stands for 'Not a Number' and is a special JavaScript value that represents the result of an invalid or undefined mathematical operation.

NaN silently corrupts calculations without throwing errors, making bugs hard to trace. Recognizing when and why NaN appears helps you validate inputs and catch problems early before they spread through your code.

💡 Always use Number.isNaN() to test for NaN—never use === comparison.

Ask Codex about NaN →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Replace isNaN() with Number.isNaN(). After parseInt/parseFloat, always check Number.isNaN(). Use isFinite() to additionally exclude Infinity.
📦 Applies To
javascript ES2015 web cli
🔗 Prerequisites
🔍 Detection Hints
isNaN\(
Auto-detectable: ✓ Yes eslint
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant