Hone logo
Hone
Problems

Javascript Object Emptiness Checker

In Javascript, it's a common task to determine if an object contains any properties. This is particularly useful when dealing with configuration objects, data fetched from APIs, or when validating user input. Being able to reliably check for an empty object ensures your code behaves correctly and avoids unexpected errors.

Problem Description

Your task is to create a Javascript function that takes a single argument, an object, and returns true if the object is considered "empty" and false otherwise.

Key Requirements:

  • The function must accept one argument: an object.
  • An object is considered empty if it has no own enumerable properties.
  • The function should return a boolean value: true for an empty object, false for a non-empty object.

Expected Behavior:

  • An object like {} should return true.
  • An object like { a: 1 } should return false.
  • An object like { [Symbol('id')]: 123 } should return true (as Symbols are not enumerable by default).
  • An object with inherited properties but no own properties should return true.

Edge Cases to Consider:

  • What if the input is not an object (e.g., null, undefined, a primitive)? Your function should handle these gracefully.
  • Objects with Symbol properties.

Examples

Example 1:

Input: {}
Output: true
Explanation: An object with no properties is empty.

Example 2:

Input: { name: "Alice", age: 30 }
Output: false
Explanation: The object has two own enumerable properties, so it is not empty.

Example 3:

Input: Object.create({ inheritedProp: 'value' })
Output: true
Explanation: The object has no own enumerable properties, only an inherited one.

Example 4:

Input: null
Output: true
Explanation: null is not an object with properties, and for the purpose of this challenge, we consider it "empty" or a non-object that can be treated as such.

Example 5:

Input: { [Symbol('id')]: 123 }
Output: true
Explanation: Symbols are not enumerable by default, so even though a Symbol property exists, the object is considered empty by the standard definition of own enumerable properties.

Constraints

  • The input will be a Javascript value.
  • The function should aim for an efficient solution, suitable for potentially large objects (though the emptiness check itself is generally fast).

Notes

Consider how you can iterate through or inspect an object's properties. There are several built-in Javascript methods that can help you achieve this. Think about the difference between own properties and inherited properties, and between enumerable and non-enumerable properties. For the purpose of this challenge, we are primarily concerned with own, enumerable properties.

Loading editor...
javascript