Is EmptyLoading saved progress…

Is Empty

Implement isEmpty(value) — return true when a value holds no contents, false otherwise. This mirrors lodash's isEmpty: it answers "does this collection have anything in it?" across every kind of value JavaScript hands you — arrays, strings, plain objects, Maps, Sets, array-likes such as the arguments object, and primitives. The catch is that "empty" means different things for different types, and primitives have a rule that surprises people the first time they hit it.

Signature

// value: any JavaScript value
// returns: boolean — true if the value is considered empty
function isEmpty(value): boolean;

Examples

// Nullish, empty string, empty collections → true
isEmpty(null);          // → true
isEmpty(undefined);     // → true
isEmpty('');            // → true
isEmpty([]);            // → true
isEmpty({});            // → true
isEmpty(new Map());     // → true
isEmpty(new Set());     // → true
// Anything with at least one entry → false
isEmpty('hi');                    // → false
isEmpty([1, 2]);                  // → false
isEmpty({ a: 1 });                // → false
isEmpty(new Map([['a', 1]]));     // → false
isEmpty(new Set([1]));            // → false
// Primitives have no enumerable own properties, so they count as empty.
isEmpty(42);     // → true
isEmpty(0);      // → true
isEmpty(true);   // → true
isEmpty(false);  // → true

Notes

  • Primitives are always empty. A number or boolean has no own enumerable properties — there is nothing to enumerate — so isEmpty treats it as empty and returns true. This matches lodash. isEmpty(0), isEmpty(42), isEmpty(NaN), and isEmpty(true) are all true. Don't special-case them as "not a collection and therefore false"; the rule is the opposite.
  • Map and Set use .size, not .length. They have no .length property — reading it gives undefined. Emptiness is value.size === 0.
  • Strings and arrays use .length. '' and [] are empty; 'a' and [0] are not.
  • Array-likes count by length. The arguments object and any { length: n } shape are empty when length === 0.
  • Objects are empty when they have no own enumerable keys. Inherited keys don't count; only the object's own keys do.
  • Don't worry about WeakMap/WeakSet (they expose no size), typed arrays, or the difference between own and inherited symbol keys — those are out of scope here.
Loading editor…