Optimal way to check if Object is Empty using Vanilla JavaScript

Abhay Jain
Dec 28, 2020

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.

--

--

Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.