Check for empty objects in Javascript
Posted by Abhishek Pednekar in Javascript
In this short article, we will learn how to check if a Javascript object is empty ({}
). This is particularly useful when checking responses from API's. We will run this test using the Object.keys()
method.
// Non-empty object
obj = {"a": 1, "b": 2, "c": 3}
// Will output 3
console.log(Object.keys(obj).length)
Check for the empty object
// Empty object
obj = {}
// Will output 0
console.log(Object.keys(obj).length)
// Will output true
console.log(Object.keys(obj).length === 0)
Why does this work?
Object.keys()
returns an array containing the keys of the object passed to it. We can therefore use the length
attribute to check the length of the resulting array.
obj = {"a": 1, "b": 2, "c": 3}
keysArray = Object.keys(obj)
// Will output ["a", "b", "c"]
console.log(keysArray)
// Will output 3
console.log(keysArray.length)
Object.keys()
is supported across all browsers.
Share