lodash.isplainobject provides the lodash isPlainObject utility function as a standalone module for checking if a value is a plain object (an object created by the Object constructor or one with a [[Prototype]] of null). The function performs sophisticated type checking by examining the object's prototype chain and constructor function, handling edge cases like host objects in older Internet Explorer versions.
npm install lodash.isplainobjectvar isPlainObject = require('lodash.isplainobject');For ES modules:
import isPlainObject from 'lodash.isplainobject';var isPlainObject = require('lodash.isplainobject');
// Check plain objects
console.log(isPlainObject({})); // => true
console.log(isPlainObject({ x: 0, y: 0 })); // => true
console.log(isPlainObject(Object.create(null))); // => true
// Check non-plain objects
console.log(isPlainObject([1, 2, 3])); // => false
console.log(isPlainObject(function() {})); // => false
console.log(isPlainObject(null)); // => false
console.log(isPlainObject(new Date())); // => false
// Check constructor function instances
function Foo() {
this.a = 1;
}
console.log(isPlainObject(new Foo())); // => falseChecks if a value is a plain object, that is, an object created by the Object constructor or one with a [[Prototype]] of null.
/**
* Checks if value is a plain object, that is, an object created by the
* Object constructor or one with a [[Prototype]] of null.
*
* @param {*} value The value to check.
* @returns {boolean} Returns true if value is a plain object, else false.
*/
function isPlainObject(value);Parameters:
value (*): The value to checkReturns:
boolean: Returns true if value is a plain object, else falseBehavior:
true for objects created via object literals ({})true for Object.create(null)true for objects created via new Object()false for arrays, functions, DOM elementsfalse for instances of custom constructor functionsfalse for null, undefined, primitivesUsage Examples:
var isPlainObject = require('lodash.isplainobject');
// Plain objects return true
isPlainObject({}); // => true
isPlainObject({ x: 0, y: 0 }); // => true
isPlainObject(Object.create(null)); // => true
isPlainObject(new Object()); // => true
// Non-plain objects return false
isPlainObject([1, 2, 3]); // => false (arrays)
isPlainObject(function() {}); // => false (functions)
isPlainObject(null); // => false (null)
isPlainObject(undefined); // => false (undefined)
isPlainObject(42); // => false (numbers)
isPlainObject('hello'); // => false (strings)
isPlainObject(true); // => false (booleans)
isPlainObject(new Date()); // => false (Date instances)
isPlainObject(/regex/); // => false (RegExp instances)
// Constructor function instances return false
function CustomConstructor() {
this.prop = 'value';
}
isPlainObject(new CustomConstructor()); // => false
// Class instances return false
class MyClass {
constructor() {
this.prop = 'value';
}
}
isPlainObject(new MyClass()); // => false