📦 Tiny utility to check if a value is a plain empty object in JavaScript or TypeScript
npm install is-object-empty2
* ✨ Simple yet robust NPM package that checks if a value is a plain empty object in JavaScript and TypeScript.
* ♻️ Works seamlessly with CommonJS, ESM and TypeScript
- Returns true only for plain objects with no own enumerable properties ({} or Object.create(null)).
- Returns false for Arrays, null, undefined, functions, symbols, BigInt, objects with keys or object with at least one own enumerable property (string or symbol)
- Correctly handles:
- Objects created with Object.create(null)
- Frozen or sealed objects
- Proxy objects (both empty and with keys)
- Objects with inherited properties
- Objects with non-enumerable properties
- Nested or deeply nested objects
``bash`
$ npm i is-object-empty2
- Returns true only for empty objects ({}), and false for arrays, null, undefined, or objects with keys.
- See examples below
javascript
const isObjectEmpty2 = require('is-object-empty2');console.log(isObjectEmpty2({})); // true
console.log(isObjectEmpty2({ a: 1 })); // false
console.log(isObjectEmpty2([])); // false
console.log(isObjectEmpty2(null)); // false
console.log(isObjectEmpty2(undefined)); // false
`ESM
`javascript
import isObjectEmpty2 from 'is-object-empty2';console.log(isObjectEmpty2({})); // true
console.log(isObjectEmpty2({ a: 1 })); // false
console.log(isObjectEmpty2([])); // false
console.log(isObjectEmpty2(null)); // false
console.log(isObjectEmpty2(undefined)); // false
`TypeScript
`javascript
import isObjectEmpty2 from 'is-object-empty2';const testValues: unknown[] = [
{},
{ a: 1 },
[],
null,
undefined,
Object.create(null),
{ nested: {} }
];
for (const value of testValues) {
const result: boolean = isObjectEmpty2(value);
console.log(
${JSON.stringify(value)} → ${result});
}/*
--| Expected values:
{} → true
{"a":1} → false
[] → false
null → false
undefined → false
{} → true
{"nested":{}} → false
*/
``