• Returns the first non-null and non-undefined value from the provided array of values. If all values in the array are null or undefined, returns the specified default value.

    Type Parameters

    • T

      The type of the values and the default value.

    Parameters

    • values: (undefined | null | T)[]

      An array of values to coalesce.

    • _default: NonNullable<T>

      The default value to return if all values in the array are null or undefined.

    Returns NonNullable<T>

    The first non-null and non-undefined value from the array, or the default value if all are null or undefined.

    const result = coalesceOrDefault([null, undefined, 'hello', 'world'], 'default');
    console.log(result); // Output: 'hello'
    const result = coalesceOrDefault([null, undefined], 'default');
    console.log(result); // Output: 'default'