I ran across this case today
if ({}) {
// This is returned as empty object is true
}
therefore need to figure out a way where {}
is false, tried calling .length
on an object I pass to the if statement, but that doesn't work.
I ran across this case today
if ({}) {
// This is returned as empty object is true
}
therefore need to figure out a way where {}
is false, tried calling .length
on an object I pass to the if statement, but that doesn't work.
You can use Object.keys() method to achieve this.
From Mozilla's Documentation:
The
Object.keys()
method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that afor-in loop
enumerates properties in the prototype chain as well).
if (Object.keys({}).length) {
console.log('Object is not Empty');
} else {
console.log('Object is Empty');
}
console.log(Object.keys({}).length);