What Is a Plain Object in Lodash isPlainObject?
The Lodash library provides the _.isPlainObject method
to determine whether a given value is a plain JavaScript object. In
Lodash, a plain object is strictly defined as an object created by the
Object constructor, an object literal ({}), or
an object with a [[Prototype]] of null. This
article covers the exact technical criteria Lodash uses to identify
plain objects, what values qualify, and which object types are
excluded.
The Technical Definition
Lodash defines a plain object as any value that meets the following internal criteria:
- Object-like Type: The value must be an object
(
typeof value === 'object') and cannot benull. - Object Tag: The internal
[[Class]]orSymbol.toStringTagmust match'[object Object]'(checked viaObject.prototype.toString.call(value)). - Prototype Check: The object's prototype must either
be
nullor directly resolve toObject.prototype.
To verify the prototype, Lodash traverses the prototype chain. If the
prototype of the object is null (such as objects created
via Object.create(null)), it returns true. If
the object has a prototype, Lodash checks whether the constructor of the
top-level prototype is the native Object constructor.
Values That Return
true
The _.isPlainObject function returns true
for objects created in the following ways:
- Object literals:
_.isPlainObject({}); // true _.isPlainObject({ a: 1, b: 2 }); // true - Objects created with
new Object():_.isPlainObject(new Object()); // true - Objects with a
nullprototype:_.isPlainObject(Object.create(null)); // true
Values That Return
false
Any value that inherits from a prototype other than
Object.prototype or null is considered a
non-plain object. The function returns false for:
- Class instances and custom constructors:
class User {} _.isPlainObject(new User()); // false function Person() {} _.isPlainObject(new Person()); // false - Built-in complex types:
_.isPlainObject([]); // false _.isPlainObject(new Date()); // false _.isPlainObject(/regex/); // false _.isPlainObject(new Map()); // false _.isPlainObject(new Set()); // false _.isPlainObject(new Error()); // false - Host objects and DOM nodes:
_.isPlainObject(window); // false _.isPlainObject(document.createElement('div')); // false - Primitives:
_.isPlainObject('hello'); // false _.isPlainObject(123); // false _.isPlainObject(null); // false _.isPlainObject(undefined); // false
Why the Distinction Matters
In JavaScript, arrays, functions, and class instances are technically
objects. However, utilities that serialize, deep clone, or merge
structured data often require simple key-value stores rather than
stateful instances or complex structures. Lodash’s
_.isPlainObject ensures that operations like deep merging
(_.merge) only traverse data dictionaries without
unintentionally mutating complex instances or invoking unintended
prototypes.