How to Check if An Object is Empty in JavaScript

const myObject = {}
Object.entries(myObject).length === 0;// 0return Object.entries(myObject).length === 0 ? true: false// true
Object.keys(myObject).length === 0;
// 0
return Object.keys(myObject).length === 0 ? true: false
// true
function isEmpty (myObject) {
return Object.keys(myObject).length === 0 ? true: false
}
isEmpty(myObject);
// true
Object.getOwnPropertyNames(myObject).length === 0;// 0if(Object.getOwnPropertyNames(myObject).length === 0) { // return something};
let result = ‘’;my.object.id // undefinedif (!myObject.id) result = ‘object is empty’console.log(result) // object is empty
_.isEmpty([1, 2, 3]);// => false_.isEmpty({});=> true
_.isEmpty({ ‘a’: 1 });
// => false
_.isEmpty(myObject)
// true

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store