#2727
Easy JavaScript Is object empty
81.8% acceptance
Mar 2, 2026
222
16
Given an object or an array, return if it is empty.
An empty object contains no key-value pairs.
An empty array contains no elements.
You may assume the object or array is the output of JSON.parse.
Solution
TypeScript
Time O(1)
Space O(1)
type JSONValue =
| null
| boolean
| number
| string
| JSONValue[]
| { [key: string]: JSONValue };
type Obj = Record<string, JSONValue> | JSONValue[];
function isEmpty(obj: Obj): boolean {
if (Array.isArray(obj)) return obj.length === 0;
return Object.keys(obj).length === 0;
}