Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 768 Bytes

check-if-a-value-is-a-plain-object.mdx

File metadata and controls

32 lines (25 loc) · 768 Bytes
category created title updated
Validator
2020-05-13
Check if a value is a plain object
2021-10-13

JavaScript version

const isPlainObject = (v) => !!v && typeof v === 'object' && (v.__proto__ === null || v.__proto__ === Object.prototype);

TypeScript version

const isPlainObject = (v: any): boolean =>
    !!v && typeof v === 'object' && (v.__proto__ === null || v.__proto__ === Object.prototype);

Examples

isPlainObject(null); // false
isPlainObject('hello world'); // false
isPlainObject([]); // false
isPlainObject(Object.create(null)); // false
isPlainObject(function () {}); // false

isPlainObject({}); // true
isPlainObject({ a: '1', b: '2' }); // true