2727. Is Object Empty
In this page, we will discuss LeetCode Problem 2727: Is Object Empty. The goal is to determine whether a given object has any properties or not.
📄 Problem Statement
Write a function isEmpty that takes an object or array and returns true if it has no key-value pairs or elements. Otherwise, return false.
💡 Example
Input: obj = {}
Output: true
Input: obj = {x: 5, y: 42}
Output: false
🧠 Explanation
The problem checks if an object has any properties or if an array has any elements.
You can use Object.keys(obj).length to get the number of keys in the object.
If the length is 0, then it's empty.
Works for both arrays and objects since arrays are objects in JavaScript.
✅ Your Solution
var isEmpty = function(obj) {
return Object.keys(obj).length === 0;
};