Check if a value is a plain object
npx @tessl/cli install tessl/npm-is-plain-obj@4.1.0is-plain-obj is a lightweight utility that checks if a value is a plain object. A plain object is one created by {}, new Object(), or Object.create(null). This package provides cross-realm detection, making it reliable across different JavaScript execution contexts.
npm install is-plain-objimport isPlainObject from 'is-plain-obj';Note: This package is ES Module only and does not support CommonJS (require()).
import isPlainObject from 'is-plain-obj';
// Plain objects return true
isPlainObject({foo: 'bar'}); // => true
isPlainObject(new Object()); // => true
isPlainObject(Object.create(null)); // => true
// Non-plain objects return false
isPlainObject([1, 2, 3]); // => false
isPlainObject(new Date()); // => false
isPlainObject(Math); // => false
isPlainObject(null); // => falseDetermines whether a value is a plain object using prototype chain analysis.
/**
* Check if a value is a plain object.
*
* An object is plain if it's created by either `{}`, `new Object()`, or `Object.create(null)`.
* Works correctly across different JavaScript execution contexts (realms).
*
* @param value - The value to check
* @returns True if the value is a plain object, false otherwise
*/
function isPlainObject<Value>(value: unknown): value is Record<PropertyKey, Value>;Parameters:
value (unknown): The value to testReturns:
boolean: true if the value is a plain object, false otherwiseRecord<PropertyKey, Value>Usage Examples:
import isPlainObject from 'is-plain-obj';
import {runInNewContext} from 'node:vm';
// Plain objects (return true)
isPlainObject({}); // => true
isPlainObject({foo: 'bar'}); // => true
isPlainObject({constructor: Object}); // => true
isPlainObject({valueOf: 0}); // => true
isPlainObject(new Object()); // => true
isPlainObject(Object.create(null)); // => true
// Cross-realm support
isPlainObject(runInNewContext('({})')); // => true
// Non-plain objects (return false)
isPlainObject(['foo', 'bar']); // => false
isPlainObject(new Date()); // => false
isPlainObject(new Map()); // => false
isPlainObject(Math); // => false
isPlainObject(JSON); // => false
isPlainObject(() => {}); // => false
isPlainObject(/regex/); // => false
// Primitives (return false)
isPlainObject(null); // => false
isPlainObject(undefined); // => false
isPlainObject('string'); // => false
isPlainObject(42); // => false
isPlainObject(true); // => false
// Class instances (return false)
class CustomClass {}
isPlainObject(new CustomClass()); // => false
// Arguments object (return false)
(function() {
isPlainObject(arguments); // => false
})();
// Objects with custom prototypes (return false)
isPlainObject(Object.create({})); // => false/**
* Generic type parameter representing the type of values in the plain object
*/
type Value = unknown;Built-in TypeScript Types Used:
PropertyKey: Built-in type representing string | number | symbolRecord<K, T>: Built-in utility type representing an object with keys of type K and values of type TThe function uses several techniques to ensure accurate detection:
nullObject.getPrototypeOf() to examine the prototype chainnull prototype (from Object.create(null))Object.prototype as prototype (from {} or new Object())null (handles cross-realm scenarios)Symbol.toStringTag or Symbol.iteratorThis function does not throw errors. It safely handles all input types and returns a boolean result for any value passed to it.