Lodash _.isNull() method
What you’ll learn
- How
_.isNull(value)maps to strictvalue === null. - Why
undefined,void 0, and typical falsy values returnfalse. - When to prefer
_.isNullover_.isNilor loose== null. - How null prototypes differ from the null primitive.
Prerequisites
You understand null is an intentional placeholder while undefined usually means “never assigned.”
- You know strict equality
===does not coerce types. - Try-it labs load lodash from the CDN.
Overview
Use _.isNull when APIs deliberately signal “cleared” via null but still distinguish missing keys (undefined)—database drivers and GraphQL responses frequently encode that split.
Strict primitive
Matches exactly one value: JavaScript null.
Undefined stays out
Unlike == null, undefined never counts.
Readable intent
Self-documenting guard versus loose equality tricks.
Syntax
_.isNull(value) - value: any value to test.
- Returns:
trueif value is strictlynull; otherwisefalse.
Literal null only
Lodash docs highlight void 0 (undefined) returning false.
import isNull from "lodash/isNull";
console.log(
"literalNull: " + isNull(null) + "\n" + // true
"undef: " + isNull(undefined) // false
); void 0 and numeric zero
void 0 yields undefined—still not null; zero remains a real value.
import isNull from "lodash/isNull";
console.log(
"void0: " + isNull(void 0) + "\n" + // false (lodash docs)
"zero: " + isNull(0) // false
); _.isNull versus _.isNil
_.isNil groups null and undefined; _.isNull singles out explicit null.
import isNull from "lodash/isNull";
import isNil from "lodash/isNil";
console.log(
"isNullUndef: " + isNull(undefined) + "\n" + // false
"isNilUndef: " + isNil(undefined) // true
); 📋 _.isNull vs related checks
| API / pattern | Behavior |
|---|---|
_.isNull(x) | true only when x === null. |
_.isNil(x) | true for null or undefined. |
x == null | Same truth set as _.isNil—not _.isNull. |
x === undefined | Ignores explicit null assignments entirely. |
Pitfalls to avoid
Missing versus null fields
JSON often drops absent keys—parsed objects expose undefined, not null, unless the payload literally contains null.
Driver defaults
Database drivers may coerce SQL NULL differently—verify whether you receive null or another sentinel.
“Null object” patterns
Placeholder objects are still objects—only the primitive null passes this helper.
❓ FAQ
Summary
- Purpose: detect the explicit
nullprimitive and nothing else. - Remember: pair with
_.isUndefinedor_.isNilwhen both absent states matter. - Next: explore more on Lodash _.isNumber().
_.isNull(value) is exactly value === null—the shortest possible predicate when you intentionally distinguish null from undefined.
6 people found this page helpful
