FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Check if an Object or Array is Empty

Check if an Object or Array is Empty

easy
By FrontendAtlas Editorial · Updated Jan 30, 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.
Examples
// Example 1
isEmpty({ x: 5, y: 42 }); // => false

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

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

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

Solution

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.

1

Approach: Handle arrays and objects separately

  1. **Array check:** If Array.isArray(obj) is true, simply return whether obj.length === 0.
  2. **Object check:** If obj is non-null and typeof obj === 'object', get its own enumerable keys with Object.keys(obj) and check if that list is empty.
  3. **Invalid input:** For any other type (number, string, boolean, etc.), throw a TypeError to signal misuse.
  4. **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

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.
Edge cases
  • `[]` → 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`.
Techniques
  • 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

  • MDN – Object.keys()
  • MDN – Array.isArray()

Similar questions

Shallow Clone (Object or Array)easyRemove DuplicateseasyUpdate Array at Index (Immutable)easy

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
← Prev←73 / 88Next →→