← Back to utilities
boolean

is-truthy

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.

Installation

npx fragmen add boolean/is-truthy

Source Code

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

Examples

isTruthy('hello');  // true
isTruthy(1);        // true
isTruthy([]);       // true
isTruthy({});       // true
isTruthy(false);    // false
isTruthy(0);        // false
isTruthy('');       // false
isTruthy(null);     // false

Related Utilities

is-falsy

boolean

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.

#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:733 B

Tags

Parameters

valueunknown

The value to check for truthiness.

Returns

boolean

True if the value is truthy, false otherwise.