is-empty
Checks if a value is empty (null, undefined, empty string, empty array, or empty object). A comprehensive emptiness check that handles multiple JavaScript types. Useful for form validation, data cleaning, and conditional rendering.
Installation
npx fragmen add validation/is-emptySource Code
/**
* Checks if a value is empty (null, undefined, empty string, empty array, or empty object).
*
* A comprehensive emptiness check that handles multiple JavaScript types.
* Useful for form validation, data cleaning, and conditional rendering.
*
* @tags validation, type-checking
* @param {unknown} value The value to check for emptiness
* @returns {boolean} True if the value is considered empty, false otherwise
*
* @example
* ```typescript
* // Primitives
* isEmpty(null); // true
* isEmpty(undefined); // true
* isEmpty(''); // true
* isEmpty(' '); // true (whitespace-only)
* isEmpty('hello'); // false
* isEmpty(0); // false
* isEmpty(false); // false
*
* // Arrays
* isEmpty([]); // true
* isEmpty([1, 2, 3]); // false
*
* // Objects
* isEmpty({}); // true
* isEmpty({ key: 'value' }); // false
*
* // Maps and Sets
* isEmpty(new Map()); // true
* isEmpty(new Set()); // true
* isEmpty(new Map([['key', 'value']])); // false
* ```
*/
export function isEmpty(value: unknown): boolean {
// null or undefined
if (value === null || value === undefined) {
return true;
}
// String (including whitespace-only strings)
if (typeof value === 'string') {
return value.trim().length === 0;
}
// Array
if (Array.isArray(value)) {
return value.length === 0;
}
// Map or Set
if (value instanceof Map || value instanceof Set) {
return value.size === 0;
}
// Date, RegExp, and other non-plain objects are never empty
if (value instanceof Date || value instanceof RegExp) {
return false;
}
// Plain object
if (typeof value === 'object') {
return Object.keys(value).length === 0;
}
// Everything else is considered non-empty (numbers, booleans, etc.)
return false;
}
Examples
// Primitives
isEmpty(null); // true
isEmpty(undefined); // true
isEmpty(''); // true
isEmpty(' '); // true (whitespace-only)
isEmpty('hello'); // false
isEmpty(0); // false
isEmpty(false); // false
// Arrays
isEmpty([]); // true
isEmpty([1, 2, 3]); // false
// Objects
isEmpty({}); // true
isEmpty({ key: 'value' }); // false
// Maps and Sets
isEmpty(new Map()); // true
isEmpty(new Set()); // true
isEmpty(new Map([['key', 'value']])); // false
Related Utilities
is-equal
validationDeep comparison of two values for equality. Recursively compares objects, arrays, dates, and primitives for deep equality. Handles circular references, special objects (Date, RegExp, Map, Set), and all primitive types. Useful for comparing complex data structures.
is-falsy
booleanChecks if a value is falsy. Returns true for JavaScript falsy values: false, 0, -0, 0n, "", null, undefined, and NaN. Useful for type-safe falsy checks and filtering operations.
is-truthy
booleanChecks if a value is truthy. Returns true for all JavaScript truthy values (anything that is not falsy). Complementary function to isFalsy, useful for filtering and validation.
safe-parse
jsonSafely parses a JSON string, returning undefined if parsing fails. Provides error-safe JSON parsing without throwing exceptions. Useful when working with untrusted input or when you want to handle parsing failures gracefully rather than with try-catch blocks.
has-path
objectChecks if a nested property path exists in an object. Safely traverses nested object properties using a dot-notation path string or an array of keys. Returns true if the path exists (even if the final value is undefined), false if any part of the path is missing.
local-storage
storageType-safe localStorage wrapper with automatic JSON serialization. Provides a simple API for storing and retrieving typed values from localStorage with automatic JSON serialization/deserialization. Handles errors gracefully and works in both browser and server environments.
Quick Actions
Tags
Parameters
valueunknownThe value to check for emptiness
Returns
booleanTrue if the value is considered empty, false otherwise