The Easiest way to check empty Objects in JavaScript

December 18, 2012 in JavaScript

It is a very common requirement that is needed by many JavaScript developers to check if an object is empty in JavaScript without having to write more than one line of code. In the old days, you usually had to make the following for … loop in a common function isEmpty in order to check if the object is empty:

function isEmpty(myObject) {
    for(var key in myObject) {
        if (myObject.hasOwnProperty(key)) {
            return false;

    return true;

Thanks to ECMAScript 5, you can now just use the Object.keys() function to check the object keys, so you can check if the myObject (for example) is empty as follows:

Object.keys(myObject).length == 0

I wish that this tip can be useful for you.

3 responses to The Easiest way to check empty Objects in JavaScript

  1. short but helpful JS tip, thank for clarifying

  2. clean and elegant solution! +1

  3. Rob said on April 2, 2015

    Useful if available but you should, almost without exception, always use === to do comparisons in javascript, not ==.

Skip to toolbar