Skip to content
Is Object Empty - 2727

Is Object Empty – 2727

🔗LC2727 🟢 Easy 🧩 Pattern – JSON and Objects

📅 Day 20/30 Days of JavaScript

Given an object or an array, return if it is empty.

  • An empty object contains no key-value pairs.
  • An empty array contains no elements.

You may assume the object or array is the output of JSON.parse.

Example

Input: obj = {"x": 5, "y": 42}
Output: false
Explanation: The object has 2 key-value pairs so it is not empty.

Input: obj = {}
Output: true
Explanation: The object doesn't have any key-value pairs so it is empty.
Code language: JavaScript (javascript)

Solution

/**
 * @param {Object|Array} obj
 * @return {boolean}
 */
var isEmpty = function (obj) {
    return Object.keys(obj).length === 0;
};Code language: JavaScript (javascript)
Back to Top