JavaScript BigInt.asIntN() Method

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

Definition

BigInt.asIntN(bits, bigint) squeezes a BigInt into a signed integer that is bits bits wide and returns the result as a new BigInt. A BigInt is JavaScript’s type for whole numbers of any size, written with an n at the end, like 255n. “Signed” means the result may be negative: 8 signed bits hold -128 to 127. When a value does not fit, it wraps around using two’s complement, the way computers store negative whole numbers in a fixed number of bits. That is why BigInt.asIntN(8, 255n) gives -1n.

Remember
// 8 signed bits hold -128n to 127n
BigInt.asIntN(8, 127n);         // 127n (fits, unchanged)
BigInt.asIntN(8, 128n);         // -128n (one past the top wraps to the bottom)
BigInt.asIntN(8, 255n);         // -1n
BigInt.asIntN(8, -129n);        // 127n
BigInt.asIntN(64, 2n ** 63n);   // -9223372036854775808n
BigInt.asIntN(8, 255);          // TypeError (255 is a Number, not a BigInt)

Syntax

asIntN() is a static method: you call it on BigInt itself and pass the value in, instead of calling it on a BigInt value.

JavaScript
BigInt.asIntN(bits, bigint)
CallWhat it does
BigInt.asIntN(8, value)Reads value as a signed byte. The result is always between -128n and 127n.
BigInt.asIntN(32, value)Keeps the low 32 bits and reads them as a signed 32-bit integer, the same range as an Int32Array element.
BigInt.asIntN(64, value)Gives the value a BigInt64Array slot would hold: from -(2n ** 63n) to 2n ** 63n - 1n.
BigInt.asIntN(0, value)Zero bits leave no room for any value, so the result is always 0n.

Part of BigInt since ES2020. It does not change the variable you pass in; it returns a new BigInt. (5n).asIntN is undefined, because the method lives on BigInt, not on BigInt values.

Library declarations
JavaScript
BigInt.asIntN(bits, bigint)   // returns a BigInt

// bits:   a whole number from 0 to 2 ** 53 - 1
// bigint: a BigInt such as 255n
//         (a string like "255" is converted like BigInt("255");
//          a plain Number such as 255 throws a TypeError)

Parameters

ParameterDescription
bitsRequired. How many bits the result may use, counting the sign bit. It is turned into a whole number first, so 8.7 acts like 8. A negative number, or anything above 2 ** 53 - 1, throws a RangeError.
bigintRequired. The BigInt to wrap. A plain Number such as 255 throws a TypeError (Node.js and Chrome say Cannot convert 255 to a BigInt). A string such as "255" is converted the same way BigInt("255") would convert it.

Return Value

Return ValueDescription
BigIntThe value wrapped into the signed range of bits bits: from -(2 ** (bits - 1)) to 2 ** (bits - 1) - 1. A value that already fits comes back unchanged.

JavaScript Examples

Example 1 — What Fits in a Signed Byte?

Feed several values into BigInt.asIntN(8, ...) and watch which ones stay the same and which ones wrap around.

JavaScript
const values = [100n, 127n, 128n, 200n, 255n, 256n, -129n];

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

How It Works

  1. Values inside -128 to 127 pass through. 100n and 127n already fit in 8 signed bits, so asIntN() returns them unchanged.
  2. One step past the top lands at the bottom. 128n wraps to -128n, and 255n (eight 1-bits) becomes -1n. The rule is: keep the value modulo 256, then subtract 256 if it is 128 or more, so 200n becomes -56n.
  3. The "n" is added by hand. Joining a BigInt with a string gives plain digits (v + "" is "100"), so the code appends "n" itself to make the BigInt values easy to spot.

Example 2 — Store an Unsigned 64-bit ID in a Signed Column

Some systems hand out 64-bit IDs from 0 to 264 - 1, but a database column or Java long is signed. asIntN(64, ...) gives the signed value that has exactly the same 64 bits.

JavaScript
const ids = [42n, 9223372036854775807n, 9223372036854775808n, 18446744073709551615n];

ids.forEach(function (id) {
  console.log(String(BigInt.asIntN(64, id)));
});

const slot = new BigInt64Array(1);
slot[0] = 18446744073709551615n;
console.log(String(slot[0]));   // -1
Try It Yourself

How It Works

  1. Small IDs do not change. 42n and 2n ** 63n - 1n (the largest signed 64-bit number) fit, so they come back as they were.
  2. The top bit becomes the sign. From 2n ** 63n upward the highest bit is 1, which a signed 64-bit integer reads as negative. The largest unsigned value, all 64 bits set, is -1.
  3. BigInt64Array does the same wrap. Writing the big value into a BigInt64Array stores -1n too, because the array element is a signed 64-bit integer. asIntN(64, ...) is that rule without the array.

Example 3 — One Number, Different Bit Widths (and the Errors)

The bit width decides the answer. This example wraps 1000n into several widths, then shows the two mistakes beginners make most.

JavaScript
const big = 1000n;

[16, 10, 8, 4, 0].forEach(function (bits) {
  console.log(bits + " bits: " + BigInt.asIntN(bits, big));
});

try {
  BigInt.asIntN(8, 1000);         // 1000 is a Number
} catch (e) {
  console.log(e.name);            // TypeError
}

try {
  BigInt.asIntN(-1, big);         // a negative bit count
} catch (e) {
  console.log(e.name);            // RangeError
}
Try It Yourself

How It Works

  1. 16 bits are plenty. A signed 16-bit integer reaches 32767, so 1000 fits and stays 1000.
  2. Narrow widths wrap. With 10 bits the range is -512 to 511, so 1000 becomes 1000 - 1024 = -24. With 8 bits it keeps 1000 % 256 = 232, and 232 - 256 is -24 again. 4 bits keep 8, which reads as -8, and 0 bits always give 0.
  3. Wrong arguments throw. A Number in the second position throws a TypeError, and a negative bit count throws a RangeError. The code prints only e.name, because the message text differs between browsers.

Browser & Runtime Support

BigInt.asIntN() arrived in ES2020 together with the BigInt type itself. Every engine that has BigInt also has this method.

✓ Baseline · ES2020

BigInt.asIntN()

Works in all modern browsers and in Node.js 10.4 and later, Deno, and Bun. Only very old browsers such as Internet Explorer lack BigInt completely.

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.asIntN() Excellent

Bottom line: Safe to use in any current browser or server runtime. If you must support a browser without BigInt, check typeof BigInt === "function" first, because BigInt literals such as 255n are a syntax error there.

Frequently Asked Questions

It keeps only the lowest bits of a BigInt and reads them as a signed number of that size. Values that fit are returned unchanged; bigger ones wrap around, so BigInt.asIntN(8, 255n) is -1n.
An 8-bit signed integer can only hold -128 to 127. The value 128 sets the top bit, and in two's complement the top bit means negative, so the same bits read as -128.
No. asIntN is a static method of BigInt, so (5n).asIntN is undefined. Always write BigInt.asIntN(8, 5n).
asIntN() reads the kept bits as signed, so the result can be negative. asUintN() reads the same bits as unsigned, so the result is never negative: asIntN(8, 255n) is -1n but asUintN(8, 255n) is 255n.
No. The second argument must be a BigInt; BigInt.asIntN(8, 255) throws a TypeError. Convert first with BigInt(255) or write the literal 255n.

Next: BigInt.asUintN()

Wrap values into unsigned bits instead, for hashes and counters that never go negative.

asUintN() 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