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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code