Helpful
    Preparing search index...

    Function firstTruthy

    • Returns the first value in args such that args is truthy

      Type Parameters

      • T = any

        The type of data that will be enumerated.

      Parameters

      • fallback: T

        The fallback value in the case that all values in args are falsy.

      • first: T | null | undefined

        The first value to check.

      • ...remaining: (T | null | undefined)[]

        The remaining values beyond the first to check.

      Returns T

      The first value if it is truthy. If first is falsy, then the first item in remaining such that remaining[i] is truthy is returned. If first and all values of remaining are falsy, then fallback is returned.

      // 'Defined'
      const shouldBeDefined = firstTruthy('Fallback', null, undefined, 'Defined');
      // 'Fallback'
      const shouldBeFallback = firstTruthy('Fallback', '', undefined);
      const shouldAlsoBeFallback = firstDefined('Fallback', 0, false);
      // 'First'
      const shouldBeFirst = firstDefined('Fallback', 'First', null, false, 0, NaN);