JavaScript BigInt.asUintN() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Static method

Definition

BigInt.asUintN(bits, bigint) squeezes a BigInt into an unsigned integer that is bits bits wide and returns it as a new BigInt. “Unsigned” means there is no minus sign: 8 unsigned bits hold 0 to 255. A value that does not fit wraps around like a clock: the result is the value modulo 2bits, always between 0n and 2n ** bits - 1n. Negative values wrap too, so BigInt.asUintN(8, -1n) is 255n. It is the tool for imitating the fixed-size unsigned integers of C, Rust, or Go, such as uint32 and uint64.

Remember
// 8 unsigned bits hold 0n to 255n
BigInt.asUintN(8, 255n);                // 255n (fits, unchanged)
BigInt.asUintN(8, 256n);                // 0n (one past the top wraps to 0)
BigInt.asUintN(8, -1n);                 // 255n
BigInt.asUintN(32, 2n ** 32n + 5n);     // 5n
BigInt.asUintN(64, -1n);                // 18446744073709551615n (largest uint64)

Syntax

asUintN() is a static method, called on BigInt itself with the bit width first and the value second.

JavaScript
BigInt.asUintN(bits, bigint)
CallWhat it does
BigInt.asUintN(8, value)Keeps the value as one unsigned byte: always 0n to 255n, like a Uint8Array element.
BigInt.asUintN(32, value)Emulates 32-bit unsigned math (uint32), which many hash functions and checksums use.
BigInt.asUintN(64, value)Gives the value a BigUint64Array slot would hold: 0n to 18446744073709551615n.
BigInt.asUintN(64, -1n)A short way to get the largest unsigned 64-bit number, because -1 has every bit set.

Part of BigInt since ES2020. The result equals value & ((1n << BigInt(bits)) - 1n), a bit mask that keeps the low bits, but asUintN() says what you mean and does not need the mask math.

Library declarations
JavaScript
BigInt.asUintN(bits, bigint)   // returns a BigInt, never negative

// bits:   a whole number from 0 to 2 ** 53 - 1
// bigint: a BigInt such as 300n
//         (a plain Number such as 300 throws a TypeError)

Parameters

ParameterDescription
bitsRequired. The width of the unsigned integer, for example 8, 32, or 64. It is turned into a whole number first. A negative value, or anything above 2 ** 53 - 1, throws a RangeError.
bigintRequired. The BigInt to wrap. It may be negative or far too large; both wrap into the range. A plain Number such as 300 throws a TypeError, so write 300n or BigInt(300).

Return Value

Return ValueDescription
BigIntThe value modulo 2bits, between 0n and 2n ** BigInt(bits) - 1n. A value that already fits comes back unchanged.

JavaScript Examples

Example 1 — Turn Any Value Into an Unsigned Byte

Wrap a mix of negative, normal, and too-large values into 8 unsigned bits. Every result lands between 0 and 255.

JavaScript
const values = [-1n, -2n, -128n, 255n, 256n, 300n];

values.forEach(function (v) {
  console.log(v + "n -> " + BigInt.asUintN(8, v) + "n");
});
Try It Yourself

How It Works

  1. Negative numbers count back from 256. -1n becomes 255n and -2n becomes 254n: the result is 256 plus the negative value.
  2. Too-large numbers lose their high bits. 256n needs a ninth bit, so only zeros are left: 0n. 300n is 256 + 44, so it becomes 44n.
  3. The result is never negative. Whatever goes in, an 8-bit unsigned result is in the range 0 to 255, the same rule a Uint8Array uses when you store a number.

Example 2 — A 64-bit Counter That Wraps Around

Imitate a uint64 counter, as used by network protocols and databases: when it passes the maximum, it starts again at 0.

JavaScript
const MAX_U64 = BigInt.asUintN(64, -1n);
console.log("max: " + MAX_U64);           // max: 18446744073709551615

let counter = MAX_U64 - 1n;
for (let i = 0; i < 3; i++) {
  counter = BigInt.asUintN(64, counter + 1n);
  console.log("counter: " + counter);
}

const slot = new BigUint64Array(1);
slot[0] = MAX_U64 + 1n;
console.log("BigUint64Array: " + slot[0]);   // BigUint64Array: 0
Try It Yourself

How It Works

  1. Find the maximum without typing it. -1n has every bit set, so wrapping it into 64 unsigned bits gives the largest uint64, 264 - 1.
  2. Add, then wrap. BigInt itself never overflows, so the code adds 1 and then wraps with asUintN(64, ...). The counter goes from the maximum to 0 and on to 1.
  3. BigUint64Array agrees. Storing MAX_U64 + 1n in a BigUint64Array keeps 0 as well: the typed array applies the same modulo 264 rule.

Example 3 — Compute a 32-bit FNV-1a Hash

Hash functions are written for fixed-size unsigned integers. Here asUintN(32, ...) keeps the 32-bit FNV-1a hash exact, then a bit mask shows the same idea.

JavaScript
function fnv1a32(text) {
  let hash = 2166136261n;                          // FNV offset basis
  for (let i = 0; i < text.length; i++) {
    hash = hash ^ BigInt(text.charCodeAt(i));
    hash = BigInt.asUintN(32, hash * 16777619n);   // keep 32 bits
  }
  return hash;
}

console.log(fnv1a32("a").toString(16));       // e40c292c
console.log(fnv1a32("hello").toString(16));   // 4f9f2cab

const x = 300n;
console.log(BigInt.asUintN(8, x) === (x & 255n));   // true
Try It Yourself

How It Works

  1. Mix in one character at a time. Each character code is turned into a BigInt with BigInt() and combined with the hash using XOR (^), which works on BigInts just like on numbers.
  2. Multiply, then keep 32 bits. The product grows far beyond 32 bits, so asUintN(32, ...) throws the extra bits away, exactly like uint32 math in C. The final value prints as hex with toString(16).
  3. A mask gives the same answer. For 8 bits, x & 255n keeps the same low bits as BigInt.asUintN(8, x), so the comparison prints true.

Browser & Runtime Support

BigInt.asUintN() arrived in ES2020 with the BigInt type, so it is available wherever BigInt is.

✓ Baseline · ES2020

BigInt.asUintN()

Works in all modern browsers and in Node.js 10.4 and later, Deno, and Bun. Internet Explorer has no BigInt at all.

96% Modern browser support
Google Chrome Modern versions · Desktop & Mobile
Full support
Mozilla Firefox Modern versions · Desktop & Mobile
Full support
Apple Safari Modern versions · macOS & iOS
Full support
Microsoft Edge Modern versions · Chromium
Full support
Opera Modern versions
Full support
Samsung Internet Modern versions · Android
Full support
Internet Explorer No BigInt type
Not supported
Node.js Node 10.4 and later · checked on Node 24
Supported
Deno All releases
Supported
Bun All releases
Supported
BigInt.asUintN() Excellent

Bottom line: Safe to use in current browsers and server runtimes. For speed-critical 32-bit hashing, plain Numbers with Math.imul() and >>> 0 are faster, but asUintN() is easier to read and also handles 64 bits.

Frequently Asked Questions

A positive number. The value wraps modulo 2 to the power of bits, so BigInt.asUintN(8, -1n) is 255n and BigInt.asUintN(64, -1n) is 18446744073709551615n.
Only for values that are not negative. The % operator keeps the sign of x, so -1n % 256n is -1n, while BigInt.asUintN(8, -1n) is 255n.
Write BigInt.asUintN(64, -1n) or 2n ** 64n - 1n. Both give 18446744073709551615n, which is too large for a normal Number to store exactly.
Use asUintN() when the value can never be negative, such as hashes, checksums, unsigned IDs, and counters. Use asIntN() when you need the signed meaning of the same bits.
No. BigInt values cannot be changed. The method returns a new BigInt, so assign it, for example counter = BigInt.asUintN(64, counter + 1n).

Next: toLocaleString()

Format big numbers with thousands separators for any language and region.

toLocaleString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful