Magicsheet logo

Is Object Empty

Easy
25%
Updated 8/1/2025

Asked by 3 Companies

Topics

Is Object Empty

1. What is this problem about?

The Is Object Empty interview question is a JavaScript/TypeScript utility task. You are given an object or an array, and you need to determine if it is empty. An object is empty if it has no key-value pairs, and an array is empty if it has no elements.

2. Why is this asked in interviews?

Companies like Google, Bloomberg, and Adobe use this as a screening question for Frontend or Full-stack roles. It tests your familiarity with JavaScript-specific behaviors, such as how to iterate over object keys or check the length of an array. It evaluations your ability to write concise, modern code.

3. Algorithmic pattern used

This problem follows the Object Property Enumeration pattern.

  • For Arrays: Simply check the .length property.
  • For Objects: Use Object.keys(obj).length === 0 or a for...in loop that returns false if any key is encountered.
  • Modern approach: JSON.stringify(obj) === '{}' is also possible but significantly slower than checking keys.

4. Example explanation

  • const obj = {}: Object.keys(obj) returns []. Length is 0. Result: True.
  • const arr = [1]: arr.length is 1. Result: False.
  • const obj2 = { "a": 1 }: Object.keys(obj2) returns ["a"]. Length is 1. Result: False.

5. Common mistakes candidates make

  • Falsy confusion: Thinking if (obj) checks if an object is empty. In JS, an empty object {} is truthy.
  • Checking for null: Forgetting that null or undefined might require separate handling depending on the input constraints.
  • Prototype keys: If using for...in, forgetting that it might iterate over inherited properties unless hasOwnProperty is used.

6. Interview preparation tip

In JavaScript, Object.keys() is the standard way to check for emptiness. Be ready to explain why this is more efficient than JSON.stringify. For more complex objects, consider mentioning Object.getOwnPropertyNames() to include non-enumerable properties.

Similar Questions