Optimal way to check if Object is Empty using Vanilla JavaScript
--
Looks easy to implement using Vanilla JS instead of the library like Lodash’s function(isEmpty()).
Vanilla JavaScript is not a framework or library. It’s just regular and plain JavaScript.
Is it enough to check using Object.keys?
So just using the Object.keys
, it does return true
when the object is empty. But what happens when we create a new object instance using these other constructors.
That's not acceptable, because they are not an object, so the output should be false. We can fix this by adding a constructor check. Below is the example which explains the correct way to check an empty object.