Function isEmpty

  • Checks if the given value is empty.

    This function determines whether a value is considered "empty." A value is deemed empty if:

    • It is null or undefined.
    • It is an object with no keys.
    • It is an array with no items.
    • It is a string with no characters.
    • It is a number equal to 0.

    Type Parameters

    • T

      The type of the value being checked.

    Parameters

    • value: T

      The value to check.

    Returns boolean

    true if the value is empty, otherwise false.

    // Example 1: Checking null or undefined
    console.log(isEmpty(null)); // true
    console.log(isEmpty(undefined)); // true

    // Example 2: Checking objects
    console.log(isEmpty({})); // true
    console.log(isEmpty({ key: 'value' })); // false

    // Example 3: Checking arrays
    console.log(isEmpty([])); // true
    console.log(isEmpty([1, 2, 3])); // false

    // Example 4: Checking strings
    console.log(isEmpty('')); // true
    console.log(isEmpty('Hello, World!')); // false

    // Example 5: Checking numbers
    console.log(isEmpty(0)); // true
    console.log(isEmpty(42)); // false