Explorer
JavaScript

Write a function `isEmpty(obj)` that returns true if the object has no own enumerable properties.

Problem Statement

<p>Write a function <code>isEmpty(obj)</code> that returns true if the object has no own enumerable properties.</p>

Examples

Input: obj = {}

Output: true

Explanation: Object has no own enumerable properties.

Input: obj = { a: 1 }

Output: false

Explanation: Object contains key "a".

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Object.keys() returns an array of an object's own property names. šŸ’” Hint 2: An empty object will have Object.keys().length === 0. šŸ’” Hint 3: return Object.keys(obj).length === 0;

Editorial & Approach

Problem Overview & Intuition

To solve Check if Object is Empty, we consider the execution characteristics of JavaScript engines. Write a function `isEmpty(obj)` that returns true if the object has no own enumerable properties. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.

Step-by-Step Approach

  1. Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
  2. Choose Core Mechanism: Use modern JavaScript patterns (object.keys() returns an array of an object's own property names).
  3. Implement Logic: Handle state and transformations efficiently (an empty object will have object.keys().length === 0).
  4. Return Result: Ensure proper return format and preserve caller context if applicable.

Optimal Implementation (JavaScript)

function isEmpty(obj) {
  return Object.keys(obj).length === 0;
}

Complexity Analysis

Time Complexity O(N) linear scan over input
Space Complexity O(1) constant auxiliary space (or O(N) output)

Edge Cases & Corner Traps Handled

  • Empty or boundary inputs (empty arrays, strings, zero length).
  • Type checks and unexpected values (e.g. null, undefined, negative numbers).
  • Closure preservation and memory isolation between separate invocations.

Check if Object is Empty

Easy

Write a function isEmpty(obj) that returns true if the object has no own enumerable properties.

Example Scenarios
1Example 1
Input: obj = {}
Output: true
Explanation:

Object has no own enumerable properties.

2Example 2
Input: obj = { a: 1 }
Output: false
Explanation:

Object contains key "a".

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[true,false,false]