Function asNumber

  • Attempts to extract the numeric value from the specified value. This function will always return a number. If a number cannot be extracted from the value, returns the specified fallback.

    Parameters

    • value: unknown

      Value from which to extract number.

    • Optionalfallback: number = 0

      Optional fallback if a number cannot be extracted.

    Returns number

    The number extracted from the specified value if valid, otherwise the specified fallback.

    asNumber("12px");
    // 12

    asNumber("Hello", 20);
    // 20

    asNumber("Hello");
    // 0