To accurately check if a value is a plain object in JavaScript, verify that it is non-null (typeof value === "object" && value !== null), and check its prototype: either Object.getPrototypeOf(value) === Object.prototype (standard plain object {} or new Object()) or Object.getPrototypeOf(value) === null (null-prototype dictionary created via Object.create(null)). This rules out arrays, null, functions, dates, regexes, class instances, Map/Set, and primitives.
1. Checking Standard and Null-Prototype Plain Objects
Most plain objects inherit from Object.prototype, while dictionary-style objects created with Object.create(null) have a null prototype. Checking both covers real-world JS objects reliably.
function isPlainObject(value) {
if (typeof value !== "object" || value === null) {
return false;
}
const proto = Object.getPrototypeOf(value);
return proto === Object.prototype || proto === null;
}
console.log(isPlainObject({ a: 1 })); // true
console.log(isPlainObject(Object.create(null))); // true
console.log(isPlainObject([1, 2, 3])); // false
console.log(isPlainObject(null)); // false
console.log(isPlainObject(new Date())); // false
console.log(isPlainObject(new Map())); // false
- Why
Object.getPrototypeOf(value)matters: Arrays (Array.prototype), dates (Date.prototype), maps (Map.prototype), and custom classes (User.prototype) have different prototypes thanObject.prototype. - Supporting
Object.create(null): Allowingproto === nullhandles clean key-value lookup maps created without inheritedObject.prototypeproperties.
2. Guarding Against Edge Cases (Cross-Realm or Custom @)
In browser environments with <iframe>s or separate JS realms, or when objects define custom tags, extra checks can prevent subtle mismatches.
function isStrictPlainObject(value) {
if (!isPlainObject(value)) {
return false;
}
// Reject objects with a custom Symbol.toStringTag like { [Symbol.toStringTag]: "Map" }
const tag = Object.prototype.toString.call(value);
return tag === "[object Object]";
}
const customTagged = { [Symbol.toStringTag]: "Module" };
console.log(isPlainObject(customTagged)); // true (prototype is still Object.prototype)
console.log(isStrictPlainObject(customTagged)); // false
- When to use
isStrictPlainObject: Useful when serializing, cloning, or validating untrusted inputs that might carry customSymbol.toStringTagvalues. - Keep it simple for standard data: For normal JSON-like objects,
Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === nullis fast and clear.
3. Common Traps to Avoid
- Using
typeof value === "object"alone: In JavaScript,typeof null === "object", and arrays,Date,RegExp,Map, andSetalso havetypeof === "object". - Using
value.constructor === Object: An object created withObject.create(null)hasvalue.constructor === undefined, andvalue.constructorcan be overwritten or spoofed on regular objects. - Using
Array.isArray(value)as a replacement:Array.isArrayonly checks for arrays—it still returnsfalsefor non-array objects,null, and primitives.
TL;DR: Check that typeof value === "object" && value !== null, and confirm Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null to accurately detect plain objects while excluding arrays, null, built-in instances, and class instances.
Are you validating API/JSON payloads, writing a deep-clone/merge utility, or handling Object.create(null) dictionaries?
