Lodash _.isObjectLike() method
What you’ll learn
- How
_.isObjectLike(value)narrows values to non-nullishtypeof object. - Why functions fail here while passing
_.isObject. - Which builtins qualify (arrays, dates, regexes, boxed numbers).
- When to tighten checks with
_.isPlainObjector explicit tags.
Prerequisites
You completed or skimmed the _.isObject lesson—this helper is the tighter sibling.
- You remember
typeof null === "object"but lodash filters nullish values first. - Try-it labs load lodash from the CDN.
Overview
Use _.isObjectLike inside lodash internals and application guards when you want structural objects—excluding callables—before running tag-based helpers such as string coercions or cloned iterators.
Structural focus
Keeps arrays, plain records, and host objects that typeof as object.
Functions out
Callables use typeof function—automatically excluded.
Still broad
Follow up with _.isPlainObject when only POJOs qualify.
Syntax
_.isObjectLike(value) - value: any value to test.
- Returns:
truewhen value is notnull/undefinedandtypeof value === "object".
Plain objects and arrays
Matches lodash documentation patterns for everyday collections.
import isObjectLike from "lodash/isObjectLike";
console.log(
"pojo: " + isObjectLike({}) + "\n" + // true
"array: " + isObjectLike([1, 2, 3]) // true
); Nullish guards
null fails despite the historic typeof null === "object" bug—lodash filters first.
import isObjectLike from "lodash/isObjectLike";
console.log(
"nullVal: " + isObjectLike(null) + "\n" + // false (lodash docs)
"undef: " + isObjectLike(undefined) // false
); Functions: unlike _.isObject
Lodash docs contrast _.isObjectLike(_.noop) returning false—pair with _.isObject for the dual.
import isObjectLike from "lodash/isObjectLike";
import isObject from "lodash/isObject";
console.log(
"fnLike: " + isObjectLike(function () {}) + "\n" + // false (lodash docs)
"fnObject: " + isObject(function () {}) // true
); 📋 _.isObjectLike vs related helpers
| API | Behavior |
|---|---|
_.isObjectLike(x) | Non-nullish and typeof === "object". |
_.isObject(x) | Also allows typeof === "function". |
_.isPlainObject(x) | Subset—simple dictionaries only. |
Array.isArray(x) | Arrays only—stricter than object-like. |
Pitfalls to avoid
Elements qualify
DOM nodes typeof object—combine with tag checks when that matters.
Legacy wrappers
Object(5) looks like an object—unwrap if you expected primitives.
Still wide net
Validate shapes after this coarse gate.
❓ FAQ
Summary
- Purpose: detect values that behave like structural objects, excluding functions.
- Remember: nullish values never pass—even though
typeof nullis quirky. - Next: explore more on Lodash _.isPlainObject().
_.isObjectLike(value) is value != null && typeof value === "object"—so callables never qualify, while boxed numbers and regex literals do.
6 people found this page helpful
