Is Object Empty - Problem

In JavaScript development, you'll often need to check if an object or array is empty before processing it. This is a fundamental operation that helps prevent errors and unnecessary computations.

Given a JavaScript object or array (the result of JSON.parse), determine whether it contains any data:

  • An empty object {} contains no key-value pairs
  • An empty array [] contains no elements

Your task: Return true if the input is empty, false otherwise.

Example: {}true, {"a": 1}false, []true, [1, 2]false

Input & Output

example_1.js — Empty Object
$ Input: {}
Output: true
💡 Note: The object contains no key-value pairs, so it's considered empty
example_2.js — Non-empty Object
$ Input: {"x": 5, "y": 42}
Output: false
💡 Note: The object has 2 properties (x and y), so it's not empty
example_3.js — Empty Array
$ Input: []
Output: true
💡 Note: The array contains no elements, so it's considered empty
example_4.js — Non-empty Array
$ Input: [null, false, 0]
Output: false
💡 Note: Even though the elements are falsy values, the array still contains 3 elements, so it's not empty

Constraints

  • The input is guaranteed to be a valid JSON object or array
  • Objects can have nested objects/arrays as values
  • Arrays can contain any valid JSON values including null, false, and 0
  • Maximum depth of nesting: 100 levels

Visualization

Tap to expand
Optimal Container InspectionObject {}Size Label:Keys: 0✓ EMPTYArray []Size Label:Length: 0✓ EMPTYObject {a:1}Size Label:Keys: 1✗ NOT EMPTYInstant O(1) CheckNo iteration needed!Just read the size from metadata
Understanding the Visualization
1
Identify Container
Determine if checking an object {} or array []
2
Check Size Label
Look at the built-in size indicator (length/keys count)
3
Instant Result
Return true if size is 0, false otherwise
Key Takeaway
🎯 Key Insight: Use built-in size properties instead of manual iteration for instant O(1) empty checks
Asked in
Google 24 Amazon 18 Meta 15 Microsoft 12
28.4K Views
High Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen