Lodash _.isPlainObject() method
What you’ll learn
- How
_.isPlainObject(value)narrows values to dictionary-style objects only. - Why class instances, arrays, maps, and dates fail the check.
- How
Object.create(null)still qualifies as plain per the lodash docs. - Where this check fits in config validation and safe JSON ingestion.
Prerequisites
You completed or skimmed the _.isObjectLike lesson—this helper tightens it down to true POJOs.
- You know the difference between an object literal and a class instance.
- Try-it labs load lodash from the CDN.
Overview
Reach for _.isPlainObject when you want to confirm a value is a dictionary-style bag of keys—safe to spread, merge, or serialize—without accidentally treating arrays, dates, or class instances as configuration.
POJOs only
Accepts literals and new Object(); rejects class instances.
Null-proto OK
Object.create(null) records still pass the check.
Config-safe
Ideal gate before merging options or sanitizing JSON payloads.
Syntax
_.isPlainObject(value) - value: any value to test.
- Returns:
truewhen the value's[[Prototype]]isObject.prototypeornull; otherwisefalse.
Object literals and new Object()
Everyday dictionaries created with {} or the constructor pass cleanly—the lodash docs baseline.
import isPlainObject from "lodash/isPlainObject";
console.log(
"literal: " + isPlainObject({ x: 0, y: 0 }) + "\n" + // true
"ctor: " + isPlainObject(new Object()) // true
); Class instances and built-ins fail
Constructors set their own prototype, so the value is no longer "plain" per lodash.
import isPlainObject from "lodash/isPlainObject";
function Foo() {
this.a = 1;
}
console.log(
"fooInst: " + isPlainObject(new Foo()) + "\n" + // false (lodash docs)
"arr: " + isPlainObject([1, 2, 3]) + "\n" + // false
"map: " + isPlainObject(new Map()) // false
); Object.create(null) and nullish
Prototype-less records still count as plain; null and undefined never do.
import isPlainObject from "lodash/isPlainObject";
console.log(
"noProto: " + isPlainObject(Object.create(null)) + "\n" + // true (lodash docs)
"nullVal: " + isPlainObject(null) + "\n" + // false
"undef: " + isPlainObject(undefined) // false
); 📋 _.isPlainObject vs related checks
| API / pattern | Behavior |
|---|---|
_.isPlainObject(x) | Only literals, new Object(), or Object.create(null). |
_.isObject(x) | Loose—classes, functions, and arrays all pass. |
_.isObjectLike(x) | Non-nullish with typeof === "object"—still broad. |
Object.getPrototypeOf(x) === Object.prototype | Manual equivalent; misses the null-prototype case. |
Pitfalls to avoid
Class trap
Instances of your own classes fail—don't use this check to validate domain models.
Cross-realm objects
Lodash works across realms via tag checks, but exotic host objects can still surprise you—verify with your runtime.
Spread carefully
Confirm both sides are plain before { ...a, ...b }—arrays spread as positional values, not keys.
❓ FAQ
Summary
- Purpose: confirm a value is a dictionary-style POJO before merging, spreading, or serializing.
- Remember: class instances and built-ins fail;
Object.create(null)still passes. - Next: explore more on Lodash _.isRegExp().
_.isPlainObject(Object.create(null)) returns true—lodash treats prototype-less dictionaries as plain because they behave like fresh records, not class instances.
6 people found this page helpful
