[Home] >  Validator >  Check if an object is empty

Check if an object is empty

JavaScript version

const isEmpty = (obj) => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;

// Or for enumerable property names only
const isEmpty = (obj) => JSON.stringify(obj) === '{}';

TypeScript version

const isEmpty = (obj: object): boolean => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;

const isEmpty = (obj: object): boolean => JSON.stringify(obj) === '{}';