Helpful
    Preparing search index...

    Function firstDefined

    • Returns the first value in args such that args is not null or undefined.

      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 null/undefined.

      • 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 not null or undefined. If first is undefined or null, then the first item in remaining such that remaining[i] is not null or undefined is returned. If first and all values of remaining are null or undefined, then fallback is returned.

      // 'Defined'
      const shouldBeDefined = firstDefined('Fallback', null, undefined, 'Defined');
      // 'Fallback'
      const shouldBeFallback = firstDefined('Fallback', null, undefined);
      const shouldAlsoBeFallback = firstDefined('Fallback', undefined);
      // 'First'
      const shouldBeFirst = firstDefined('Fallback', 'First', null, 'Third');