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)
Call
What 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
Parameter
Description
bits
Required. 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.
bigint
Required. 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 Value
Description
BigInt
The 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.
Negative numbers count back from 256.-1n becomes 255n and -2n becomes 254n: the result is 256 plus the negative value.
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.
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.
Find the maximum without typing it.-1n has every bit set, so wrapping it into 64 unsigned bits gives the largest uint64, 264 - 1.
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.
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
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.
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).
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.
Compatibility
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 ChromeModern versions · Desktop & Mobile
Full support
Mozilla FirefoxModern versions · Desktop & Mobile
Full support
Apple SafariModern versions · macOS & iOS
Full support
Microsoft EdgeModern versions · Chromium
Full support
OperaModern versions
Full support
Samsung InternetModern versions · Android
Full support
Internet ExplorerNo BigInt type
Not supported
Node.jsNode 10.4 and later · checked on Node 24
Supported
DenoAll releases
Supported
BunAll 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).