Lodash _.isNil() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Lang

Definition

Checks if value is null or undefined (nullish). Part of Lodash Lang since 4.0.0 (stable — not deprecated). Note: same idea as value == null — 0, "", false, and NaN are not nil.

Pass the value you want to inspect or convert. Read the return type carefully before you use the result.

Syntax

Call _.isNil. Start with this line.

Lodash
_.isNil(value)
CallWhat it does
_.isNil(value)Primary signature.

Prefer import isNil from "lodash/isNil" when you want a smaller bundle.

Library declarations
Lodash
_.isNil(value)

Parameters

ParameterDescription
valueRequired. Value to check

Return Value

Return ValueDescription
*true if nullish, else false

Lodash Examples

Example 1 — Official: null, undefined, NaN

Only the two nullish values return true.

Lodash
console.log(_.isNil(null));   // true
console.log(_.isNil(void 0)); // true
console.log(_.isNil(NaN));    // false
Try It Yourself

How It Works

  1. null. Matches → true.
  2. void 0. Expression for undefined → true.
  3. NaN. Defined number (albeit invalid) → false.
  4. Rule. Nullish ≠ “invalid number.”

Example 2 — Falsy Values Are Not Nil

Keep 0, empty string, and false as real values.

Lodash
console.log(_.isNil(0));     // false
console.log(_.isNil(""));    // false
console.log(_.isNil(false)); // false
console.log(!0);             // true  // different idea
Try It Yourself

How It Works

  1. Zero. Valid number → not nil.
  2. Empty string. Present string → not nil.
  3. false. Boolean present → not nil.
  4. Contrast. !value treats all falsy as missing — isNil does not.

Example 3 — isNil vs isNull vs isUndefined

Split the two nullish cases when you need the difference.

Lodash
console.log(_.isNil(null));        // true
console.log(_.isNull(null));       // true
console.log(_.isUndefined(null));  // false

console.log(_.isNil(undefined));       // true
console.log(_.isNull(undefined));      // false
console.log(_.isUndefined(undefined)); // true
Try It Yourself

How It Works

  1. isNil. Catches both nullish values.
  2. isNull. Only exact null.
  3. isUndefined. Only exact undefined.
  4. Pick. Use the specific helper when APIs treat them differently.

Example 4 — Guard Optional Properties

Skip work when a field was never set.

Lodash
function labelOf(user) {
  if (_.isNil(user) || _.isNil(user.name)) {
    return "(anonymous)";
  }
  return user.name;
}

console.log(labelOf(null));            // (anonymous)
console.log(labelOf({ name: "Ada" })); // Ada
console.log(JSON.stringify(labelOf({ name: "" }))); // ""
Try It Yourself

How It Works

  1. Missing user. null → fallback label.
  2. Present name. Returns "Ada".
  3. Empty string. Not nil → returned as-is (valid empty name).
  4. Use. Prefer this over !user.name when "" is allowed.

Example 5 — Apply a Default Only When Nil

Replace missing values without wiping 0 or false.

Lodash
function withDefault(value, fallback) {
  return _.isNil(value) ? fallback : value;
}

console.log(withDefault(undefined, 10)); // 10
console.log(withDefault(0, 10));         // 0
console.log(withDefault(null, 10));      // 10
Try It Yourself

How It Works

  1. undefined. Nil → use fallback 10.
  2. Zero. Not nil → keep 0.
  3. null. Nil → fallback again.
  4. Why. Safer than value || fallback, which would replace 0.

Browser & Runtime Support

_.isNil is a Lodash 4.x Lang helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native engines express the same idea with value == null or nullish coalescing (??), but not this helper by name.

✓ Stable · Lodash 4.0.0+

_.isNil()

Available in [email protected] full builds and the lodash/isNil modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.isNil Lodash 4.0.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import isNil from "lodash/isNil" for smaller builds. Equivalent to value == null as a named function.

Frequently Asked Questions

_.isNil(value) returns true when value is null or undefined — the two nullish values. Everything else, including 0, "", false, and NaN, returns false.
No. Falsy values like 0, "", and false are not nil. Only null and undefined count.
No. NaN is a defined numeric value. _.isNil(NaN) is false — matching the official Lodash docs.
isNull checks only null. isUndefined checks only undefined. isNil matches either one (like value == null).
Yes — same semantics. Lodash gives you a named helper for readability and chaining with other lang guards.
Use import isNil from "lodash/isNil" or require("lodash/isNil") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.isNil.

Next: _.isNull()

Check whether a value is exactly null.

_.isNull() tutorial →

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.

10 people found this page helpful