How do you accurately check if a value is a plain object in JavaScript? | A1Lab@FahadM to JavaScriptEnglish • 5 days ago

How do you accurately check if a value is a plain object in JavaScript?

message-square
0

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 than Object.prototype.
  • Supporting Object.create(null): Allowing proto === null handles clean key-value lookup maps created without inherited Object.prototype properties.

2. Guarding Against Edge Cases (Cross-Realm or Custom @@toStringTag)

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 custom Symbol.toStringTag values.
  • Keep it simple for standard data: For normal JSON-like objects, Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null is fast and clear.

3. Common Traps to Avoid

  • Using typeof value === "object" alone: In JavaScript, typeof null === "object", and arrays, Date, RegExp, Map, and Set also have typeof === "object".
  • Using value.constructor === Object: An object created with Object.create(null) has value.constructor === undefined, and value.constructor can be overwritten or spoofed on regular objects.
  • Using Array.isArray(value) as a replacement: Array.isArray only checks for arrays—it still returns false for 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?