← Back to utilities
boolean

is-falsy

Checks 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.

Installation

npx fragmen add boolean/is-falsy

Source Code

/**
 * Checks 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.
 *
 * @tags pure, validation, type-checking
 * @param {unknown} value The value to check for falsiness.
 * @returns {boolean} True if the value is falsy, false otherwise.
 *
 * @example
 * ```typescript
 * isFalsy(false);     // true
 * isFalsy(0);         // true
 * isFalsy('');        // true
 * isFalsy(null);      // true
 * isFalsy(undefined); // true
 * isFalsy('hello');   // false
 * isFalsy(1);         // false
 * isFalsy([]);        // false
 * ```
 */
export function isFalsy(value: unknown): boolean {
  return !value;
}

Examples

isFalsy(false);     // true
isFalsy(0);         // true
isFalsy('');        // true
isFalsy(null);      // true
isFalsy(undefined); // true
isFalsy('hello');   // false
isFalsy(1);         // false
isFalsy([]);        // false

Related Utilities

is-truthy

boolean

Checks 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.

#pure#validation#type-checking

safe-parse

json

Safely 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.

#pure#validation

has-path

object

Checks 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.

#pure#validation

is-empty

validation

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.

#validation#type-checking

is-equal

validation

Deep 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.

#validation#pure

chunk

array

Splits an array into chunks of a specified size. Creates a new array containing subarrays (chunks) of the original array, each with a maximum length of the specified size. The last chunk may contain fewer elements if the array length is not evenly divisible by the chunk size.

#pure#array-manipulation

Quick Actions

Estimated size:729 B

Tags

Parameters

valueunknown

The value to check for falsiness.

Returns

boolean

True if the value is falsy, false otherwise.