Check if an Object or Array is Empty

easy
By FrontendAtlas Editorial · Updated Oct 10, 2026
Implement isEmpty(obj) that returns true only if the input is an object or array with no own keys/elements, and false otherwise. For non-object/array inputs (like numbers or strings), the function should throw a TypeError. Concepts: objects, arrays, validation.

Arguments

  • obj: Object | Array — An object or array parsed from JSON.

Returns

boolean — Returns true if the object/array is empty; otherwise false.

Solution idea

Goal: implement isEmpty(obj) so that it returns true only when the input is an array with length 0 or an object whose own enumerable keys list is empty, and throws a TypeError for non-object/array inputs. Key concepts: objects, arrays, validation.

Examples
// Example 1
isEmpty({ x: 5, y: 42 }); // => false

// Example 2
isEmpty({}); // => true

// Example 3
isEmpty([null, false, 0]); // => false

// Example 4
isEmpty([]); // => true