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.

--

--

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

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
Abhay Jain

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