TobilobaCodes – LeetCode Explorer

2727. Is Object Empty

Given a value that is either an object or array (as output by JSON.parse), determine whether it is empty.

📄 Problem Statement

Implement a function isEmpty(obj) that returns:

Assume that the input is always a valid object or array (never null, number, string, etc).

💡 Example


isEmpty({}) ➞ true  
isEmpty([]) ➞ true  
isEmpty({ "key": "value" }) ➞ false  
isEmpty([1, 2, 3]) ➞ false

🧠 Explanation

Whether it's an array or object, we use Object.keys(obj) to get an array of its own property names. If the length is zero, then it's empty.

This works for both arrays and objects because both are technically objects in JavaScript and will return their enumerable keys.

✅ Your Solution


/**
 * @param {Object|Array} obj
 * @return {boolean}
 */
var isEmpty = function(obj) {
    return Object.keys(obj).length === 0;
};