Skip to main content

isPlainObject()

isPlainObject(value): value is Record<string, any>

Checks if a value is a plain object (object literal or Object.create(null)).

๐Ÿ’Ž Why is this a Hidden Gem?

Distinguishes true dictionaries from arrays, null, or class instances.

note

Excludes arrays, Date, Map, Set, class instances, etc. Only {}, new Object(), and Object.create(null).


Parametersโ€‹

value: unknownโ€‹

The value to check.


Returns: value is Record<string, any>โ€‹

true if the value is a plain object, false otherwise.


See Alsoโ€‹

isObject


Sinceโ€‹

1.0.0


Exampleโ€‹

isPlainObject({});                    // => true
isPlainObject({ a: 1 }); // => true
isPlainObject(new Object()); // => true
isPlainObject(Object.create(null)); // => true
isPlainObject([]); // => false
isPlainObject(new Date()); // => false
isPlainObject(new Map()); // => false
isPlainObject(new (class Foo {})()); // => false
isPlainObject(null); // => false

How it works?โ€‹

Type guard that checks if a value is a plain object (object literal or Object.create(null)).

What Qualifiesโ€‹

ValueResultReason
{}checkmark trueObject literal
{ a: 1 }checkmark trueObject literal
new Object()checkmark trueObject constructor
Object.create(null)checkmark trueNull prototype
[]cross falseArray
new Date()cross falseDate instance
new Map()cross falseMap instance
new MyClass()cross falseClass instance
nullcross falseNot an object

isPlainObject vs isObjectโ€‹


Use Casesโ€‹

Identify configuration objects ๐Ÿ“Œโ€‹

Ensure a value is a simple dictionary {} and not a complex object instance. Perfect for merging configs or validating JSON payloads.

if (isPlainObject(config)) {
// Safe to merge or spread
const merged = { ...defaultConfig, ...config };
}

Deep clone decision logicโ€‹

Determine if a value needs recursive cloning or can be copied by reference.

function deepClone(value: unknown): unknown {
if (!isPlainObject(value)) {
return value; // Primitives, arrays handled elsewhere, class instances kept as-is
}

const result: Record<string, unknown> = {};
for (const key in value) {
result[key] = deepClone(value[key]);
}
return result;
}