Lodash _.isUndefined() method
What you’ll learn
- How
_.isUndefined(value)performs a strict=== undefinedcheck. - Why
null,0, and""are not considered undefined. - How to choose between
_.isUndefinedand_.isNil. - Where this guard fits in default-value and optional-prop handling.
Prerequisites
You know the difference between undefined (no value assigned) and null (intentional empty).
- You have read or written
typeof x === "undefined"guards before. - Try-it labs load lodash from the CDN.
Overview
Use _.isUndefined before applying default values, branching on optional parameters, or skipping serialization for unset object properties—without lumping in explicit null.
Strict identity
Returns true only when value is the undefined primitive.
Not null
null is intentional; pair with _.isNil when both should match.
Defaults helper
Ideal companion to _.defaults / ?? coalescing.
Syntax
_.isUndefined(value) - value: any value to test.
- Returns:
truewhenvalue === undefined; otherwisefalse.
Bare undefined and void 0
The lodash docs baseline: void 0 evaluates to undefined and passes the guard.
import isUndefined from "lodash/isUndefined";
let unset;
console.log(
"void0: " + isUndefined(void 0) + "\n" + // true (lodash docs)
"unset: " + isUndefined(unset) + "\n" + // true
"literal: " + isUndefined(undefined) // true
); null is not undefined
Lodash docs explicitly contrast: _.isUndefined(null) returns false.
import isUndefined from "lodash/isUndefined";
console.log(
"nullVal: " + isUndefined(null) + "\n" + // false (lodash docs)
"defined: " + isUndefined("Hello") // false
); Falsy values and missing properties
Numeric zero, empty strings, and false are valid values—only the address property below is genuinely undefined.
import isUndefined from "lodash/isUndefined";
const user = { name: "John", age: 30 };
console.log(
"zero: " + isUndefined(0) + "\n" + // false
"empty: " + isUndefined("") + "\n" + // false
"addrMissing: " + isUndefined(user.address) // true
); 📋 _.isUndefined vs related checks
| API / pattern | Behavior |
|---|---|
_.isUndefined(x) | true only when x === undefined. |
x === undefined | Identical semantics—lodash wraps the idiom. |
typeof x === "undefined" | Also matches undeclared identifiers (no ReferenceError). |
_.isNil(x) | Matches both null and undefined. |
Pitfalls to avoid
|| trap
value || fallback also overrides 0, "", and false. Prefer _.isUndefined or nullish coalescing ?? for accurate defaults.
Sparse arrays
Holes in arrays read as undefined but behave subtly differently from explicit arr[i] = undefined.
Reference errors
Reading a truly undeclared identifier throws before lodash runs—use typeof if you must probe global existence.
❓ FAQ
Summary
- Purpose: recognize values strictly equal to
undefined. - Remember:
null,0,"", andfalseare all not undefined. - Next: explore more on Lodash _.isWeakMap().
Lodash implements _.isUndefined as a one-liner: value === undefined. Historically the helper guarded against legacy environments where undefined could be reassigned—modern strict-mode JS prevents that.
6 people found this page helpful
