Check if an Object or Array is Empty
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 — 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.
// Example 1
isEmpty({ x: 5, y: 42 }); // => false
// Example 2
isEmpty({}); // => true
// Example 3
isEmpty([null, false, 0]); // => false
// Example 4
isEmpty([]); // => trueSolution
Overview
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.
Approach: Handle arrays and objects separately
- **Array check:** If
Array.isArray(obj)is true, simply return whetherobj.length === 0. - **Object check:** If
objis non-null andtypeof obj === 'object', get its own enumerable keys withObject.keys(obj)and check if that list is empty. - **Invalid input:** For any other type (number, string, boolean, etc.), throw a
TypeErrorto signal misuse. - **Return result:** Return a boolean indicating emptiness for arrays/objects.
export default function isEmpty(obj) {
if (Array.isArray(obj)) return obj.length === 0;
if (obj && typeof obj === 'object') return Object.keys(obj).length === 0;
throw new TypeError('Expected object or array');
}
export default function isEmpty(obj: Record<string, any> | any[]): boolean {
if (Array.isArray(obj)) return obj.length === 0;
if (obj && typeof obj === 'object') return Object.keys(obj).length === 0;
throw new TypeError('Expected object or array');
}
Notes & Pitfalls
- Checking `Object.keys()` on `null` or `undefined` will throw — always guard with `obj && typeof obj === 'object'`.
- Strings also have `.length`, but should not be considered valid inputs here (this implementation throws `TypeError` for them).
- Prototype-inherited properties are ignored by `Object.keys()`, which is correct for this task.
- `[]` → true (no elements).
- `{}` → true (no keys).
- `[null, false, 0]` → false (contains items even if falsy).
- `Object.create(null)` → true (no enumerable keys).
- Non-object/array inputs like `42` or `'hi'` → `TypeError`.
- Use `Array.isArray()` for accurate array detection.
- Prefer `Object.keys()` over `for...in` to ignore prototype properties.
Common mistakes on this challenge
isEmpty should branch arrays, objects, and invalid inputs
This utility needs explicit runtime type handling before checking emptiness.
- Return `obj.length === 0` for arrays.
- For plain objects, use `Object.keys(obj).length === 0`.
- Throw `TypeError` for non-object/non-array inputs.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.