Skip to main content

Try it Live

Run Uint examples in the interactive playground

    Safe Integer Range

    JavaScript numbers are 64-bit floats. Only integers in range [-2^53 + 1, 2^53 - 1] are safe.Values exceeding Number.MAX_SAFE_INTEGER (9007199254740991) will throw.
    // Safe values
    Uint.toNumber(Uint(0n))                         // 0
    Uint.toNumber(Uint(9007199254740991n))         // OK: MAX_SAFE_INTEGER
    
    // Unsafe values throw
    Uint.toNumber(Uint(9007199254740992n))         // Error: too large
    Uint.toNumber(Uint(10n ** 18n))                // Error: too large
    

    Usage Patterns

    Array Indices

    const index = Uint(5n)
    const arr = [1, 2, 3, 4, 5, 6]
    const value = arr[Uint.toNumber(index)]  // 6
    

    Safe Conversion

    function toNumberSafe(uint: BrandedUint256): number | null {
      try {
        return Uint.toNumber(uint)
      } catch {
        return null
      }
    }
    

    See Also