Lodash _.isNil() method

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

What you’ll learn

  • How _.isNil(value) spots exactly null and undefined.
  • Why empty strings, numeric zero, and false are not considered nil.
  • How this differs from broad falsy checks and some TypeScript assumptions.
  • Where nil guards fit in defensive defaults and API normalization.

Prerequisites

You know null is intentional absence while undefined often means “missing.”

  • You have seen loose equality value == null used as an idiom.
  • Try-it labs load lodash from the CDN.

Overview

Use _.isNil before dereferencing optional props, merging configuration objects, or short-circuiting pipelines—without rejecting legitimate falsy business values.

Dual nullish

Single check covers both null and undefined.

Keeps 0 / \"\"

Falsy-but-valid inputs survive unlike naive if (!x) traps.

Readable guard

Pairs cleanly with lodash flows after _.get results.

Syntax

javascript
_.isNil(value)
  • value: any value to test.
  • Returns: true when value is null or undefined; otherwise false.
1

null and undefined

Both literal forms register as nil—the lodash docs baseline.

javascript
import isNil from "lodash/isNil";

console.log(
  "nullVal: " + isNil(null) + "\n" +           // true
  "undef: " + isNil(undefined)               // true
);
Try it Yourself
2

Falsy—but not nil—values

Numeric zero, empty strings, and NaN remain present values per lodash.

javascript
import isNil from "lodash/isNil";

console.log(
  "zero: " + isNil(0) + "\n" +           // false
  "emptyStr: " + isNil("") + "\n" +       // false
  "nan: " + isNil(NaN)                   // false (lodash docs)
);
Try it Yourself
3

void 0 and boolean false

void 0 evaluates to undefined; explicit false must stay usable.

javascript
import isNil from "lodash/isNil";

console.log(
  "void0: " + isNil(void 0) + "\n" +       // true
  "falsePrim: " + isNil(false)           // false
);
Try it Yourself

📋 _.isNil vs related checks

API / patternBehavior
_.isNil(x)true only for null or undefined.
x == nullIdentical semantics—lodash wraps the idiom.
x === undefinedMisses explicit null assignments.
!xAlso catches 0, \"\", false, NaN.

Pitfalls to avoid

Defaults

|| pitfalls

value || fallback nukes valid 0; combine nil checks with nullish coalescing (??) when appropriate.

Arrays

Sparse holes

Missing indices read as undefined yet behave subtly differently from explicitly stored undefined.

Docs

NaN discipline

Use _.isNaN when you specifically need numeric NaN detection.

❓ FAQ

No. Falsy values like 0, "", and false are not nil—only null and undefined count.
No. NaN is still a defined numeric primitive per lodash docs; _.isNil(NaN) is false.
Readability and consistent chaining alongside other lodash guards—not different semantics.
Older exotic objects historically behaved oddly with == null; rely on ordinary null/undefined values for predictable guards.

Summary

  • Purpose: recognize JavaScript nullish values without broader falsy coupling.
  • Remember: implementation is value == null.
  • Next: explore more on Lodash _.isNull().
Did you know?

_.isNil(value) is implemented as value == null, so both null and undefined match—the same idiom many teams use for optional guards.

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