Lodash _.isNaN() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.isNaN(value) isolates numeric NaN without coercion traps.
  • Why lodash differs from legacy isNaN(undefined) surprises.
  • How boxed Number(NaN) objects behave versus Number.isNaN.
  • When to pair NaN checks with _.isFinite or type guards.

Prerequisites

You know NaN is the only value where x !== x for primitives.

  • You have seen global isNaN coerce operands before testing.
  • Try-it labs load lodash from the CDN.

Overview

Use _.isNaN when sanitizing math pipelines, parsed floats, or DOM-measured metrics—anywhere legacy isNaN might mislabel non-numbers.

Numeric gate

_.isNumber runs first—strings miss the NaN branch.

Boxed NaN

Object(NaN) unwraps via unary plus for detection.

No coercion

Avoids global isNaN truthy quirks on undefined.

Syntax

javascript
_.isNaN(value)
  • value: any value to test.
  • Returns: true when value is a numeric NaN (primitive or boxed); otherwise false.
1

Primitive and boxed NaN

Lodash matches the documented behavior for both forms.

javascript
import _isNaN from "lodash/isNaN";

console.log(
  "nanPrim: " + _isNaN(NaN) + "\n" +           // true
  "boxedNaN: " + _isNaN(Object(NaN))             // true
);
Try it Yourself
2

undefined: lodash vs global isNaN

Global isNaN coerces undefined to NaN; lodash rejects non-numbers first.

javascript
import _isNaN from "lodash/isNaN";

console.log(
  "lodashUndef: " + _isNaN(undefined) + "\n" +              // false
  "globalUndef: " + globalThis.isNaN(undefined)            // true
);
Try it Yourself
3

Infinity and ordinary numbers

Non-NaN numeric values—including infinities—return false.

javascript
import _isNaN from "lodash/isNaN";

console.log(
  "infinity: " + _isNaN(Infinity) + "\n" +   // false
  "fortyTwo: " + _isNaN(42)                   // false
);
Try it Yourself

📋 _.isNaN vs related checks

APIBehavior
_.isNaN(x)true for primitive NaN and boxed Number(NaN); gated by _.isNumber.
Number.isNaN(x)true only for primitive NaN—boxed NaN objects return false.
global isNaN(x)Coerces with ToNumber; often true for undefined and "NaN".
x !== xPrimitive NaN trick only—misses boxed NaN and reads oddly in reviews.

Pitfalls to avoid

Parsing

Strings before Number()

_.isNaN("NaN") is false—convert with Number() first if you intentionally parse.

Naming

Shadowing global

Importing as isNaN hides the built-in; alias like _isNaN when comparing behaviors.

Validation

Finite vs NaN

NaN is still a number type—pair with _.isFinite when rejecting non-usable math values.

❓ FAQ

No. Global isNaN coerces values to numbers first, so undefined and many strings become true. Lodash follows Number-style intent: only actual numeric NaN values.
Not always—lodash also treats boxed Number(NaN) as NaN, while Number.isNaN(Object(NaN)) is false.
undefined is not classified as a number by lodash isNumber, so the NaN shortcut never applies.
No—the string is not a number object to lodash, even though global isNaN("NaN") coerces to numeric NaN.

Summary

  • Purpose: detect numeric NaN without global coercion mistakes.
  • Remember: non-numbers—including undefined—exit as false.
  • Next: explore more on Lodash _.isNative().
Did you know?

_.isNaN implements _.isNumber(value) && value != +value—so only numeric NaN values qualify; undefined and strings return false, unlike global isNaN coercing them.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful