🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › BigInt
BigInt - JavaScript | MDN
A BigInt value, also sometimes just called a BigInt, is a bigint primitive, created by appending n to the end of an integer literal, or by calling the BigInt() function (without the new operator) and giving it an integer value or string value.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › BigInt64Array
BigInt64Array - JavaScript | MDN
The BigInt64Array typed array represents an array of 64-bit signed integers in the platform byte order. If control over byte order is needed, use DataView instead. The contents are initialized to 0n unless initialization data is explicitly provided. Once established, you can reference elements ...
🌐
W3Schools
w3schools.com › js › js_bigint.asp
JavaScript BigInt
JSON Intro JSON Syntax JSON vs ... Prep JS Bootcamp JS Certificate JS Reference ... BigInt is a JavaScript data type for handling and storing big integer values....
🌐
Coolaj86
coolaj86.com › articles › convert-js-bigints-to-typedarrays
How to go between JS BigInts and TypedArrays
We now have native BigInts in JavaScript (not just 64-bit integers, but any arbitrary precision of integer with no pre-defined bit-width).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-bigint
JavaScript BigInt - GeeksforGeeks
December 4, 2025 - JavaScript BigInt is a built-in object that represents whole numbers larger than (2^{53} - 1). A BigInt value, also known as a bigint primitive, is created by appending n to an integer literal or by calling the BigInt() function with an integer ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › BigInt64Array › BigInt64Array
BigInt64Array() constructor - JavaScript | MDN
// From a length const bigint64 = new BigInt64Array(2); bigint64[0] = 42n; console.log(bigint64[0]); // 42n console.log(bigint64.length); // 2 console.log(bigint64.BYTES_PER_ELEMENT); // 8 // From an array const x = new BigInt64Array([21n, 31n]); console.log(x[1]); // 31n // From another TypedArray const y = new BigInt64Array(x); console.log(y[0]); // 21n // From an ArrayBuffer const buffer = new ArrayBuffer(64); const z = new BigInt64Array(buffer, 8, 4); console.log(z.byteOffset); // 8 // From an iterable const iterable = (function* () { yield* [1n, 2n, 3n]; })(); const bigint64FromIterable = new BigInt64Array(iterable); console.log(bigint64FromIterable); // BigInt64Array [1n, 2n, 3n] JavaScript typed arrays guide ·
🌐
Scaler
scaler.com › home › topics › javascript bigint library
JavaScript BigInt Library - Scaler Topics
September 29, 2023 - The JavaScript BigInt is a built-in object in javascript that is used to store the numbers that are greater than Number.MAX_SAFE_INTEGER. Learn about the uses and functions under the BigInt Library in JavaScript with Scaler Topics.
Top answer
1 of 4
18

ECMAScript 2020 now has a built-in BigInt type, with BigInt64Array and BigUint64Array typed arrays. The internal 64-bit representations are converted to and from BigInt values, which are needed to keep the full precision.

BigInt and the array types are still relatively new, so see below if you need to support older browsers or Node versions. You can use resources like CanIUse.com to see what browsers to help you decide if it's an option or not. Polyfills could also be an option as a workaround until you phase out support for unsupported browsers.


Answer for older browsers/Node environments:

There's no practical way to implement an Int64Array, because all numbers in JavaScript are 64-bit floating point numbers, which only have 53 bits of precision. Like Simeon said in his comment, you could use a big integer library, but it would be much slower.

If you really need an array of 64-bit integers, regardless of performance, the Google Closure library has a 64-bit Long class that I would imagine is faster than a more general big integer library. I've never used it though, and I don't know if you can separate it easily from the rest of the library.

2 of 4
2

You can safely read a number below 2^53-1 (aka 0x1fffffffffffff or 9007199254740991), but not above that. Below is the code for doing this.

As said you cannot safely go beyond the 2^53-1 integer with Javascript numbers, because in Javascript numbers are really always represented as double precision 64 bit floating point numbers. These represent numbers as a base and a exponent within these 64 bits, with 53 bits for the base and the rest being for the exponent, so you'll lose precise integer precision when you go beyond 53 bits and need to use the exponent.

But here's at least how you can check for whether an unsigned 64 bit long integer in a Uint8Array is less than 2^53-1 and then safely read it if you want:

function getUint64(inputArray, index, littleEndian) {
  const dataView = new DataView(inputArray.buffer);
  let hi = dataView.getUint32(index, littleEndian);
  let lo = dataView.getUint32(index + 4, littleEndian);
  if (littleEndian) {
    const tmp = hi;
    hi = lo;
    lo = tmp;
  }
  if (hi > 0x1fffff) {
    throw new Error(
      'Cannot safely parse uints over 2^53 - 1 (0x1fffffffffffff) in to a 64 bit float.'
    );
  }
  const numberValue = (hi * 0x100000000) + lo;
  return numberValue;
}

// Tests gotten from this other excellent answer here: https://stackoverflow.com/a/53107482/628418
// [byteArray, littleEndian, expectedValue, expectError]
const testValues = [
  // big-endian
  [new Uint8Array([0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0xff]),  false, 255], 
  [new Uint8Array([0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0xff, 0xff]),  false, 65535],
  [new Uint8Array([0x00, 0x00, 0x00, 0x00,  0xff, 0xff, 0xff, 0xff]),  false, 4294967295],
  [new Uint8Array([0x00, 0x00, 0x00, 0x01,  0x00, 0x00, 0x00, 0x00]),  false, 4294967296],
  [new Uint8Array([0x00, 0x1f, 0xff, 0xff,  0xff, 0xff, 0xff, 0xff]),  false, 9007199254740991], // maximum precision
  [new Uint8Array([0x00, 0x20, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00]),  false, 9007199254740992, true], // precision lost
  [new Uint8Array([0x00, 0x20, 0x00, 0x00,  0x00, 0x00, 0x00, 0x01]),  false, 9007199254740992, true], // precision lost

  // little-endian
  [new Uint8Array([0xff, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00]),  true, 255], 
  [new Uint8Array([0xff, 0xff, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00]),  true, 65535],
  [new Uint8Array([0xff, 0xff, 0xff, 0xff,  0x00, 0x00, 0x00, 0x00]),  true, 4294967295],
  [new Uint8Array([0x00, 0x00, 0x00, 0x00,  0x01, 0x00, 0x00, 0x00]),  true, 4294967296],
  [new Uint8Array([0x00, 0x00, 0x00, 0x00,  0x00, 0x01, 0x00, 0x00]),  true, 1099511627776],
  [new Uint8Array([0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x01, 0x00]),  true, 281474976710656],
  [new Uint8Array([0xff, 0xff, 0xff, 0xff,  0xff, 0xff, 0x1f, 0x00]),  true, 9007199254740991], // maximum precision
];

testValues.forEach(testGetUint64);

function testGetUint64([bytes, littleEndian, expectedValue, expectError]) {
  if (expectError) {
    try {
      const val = getUint64(bytes, 0, littleEndian);
      console.error('did not get the expected error');
    } catch(error) {
      console.log('got expected error: ' + error.message);
    }
  } else {
    const val = getUint64(bytes, 0, littleEndian);
    console.log(val === expectedValue? 'pass' : 'FAIL. expected '+expectedValue+', received '+val);
  }
}

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › BigUint64Array
BigUint64Array - JavaScript | MDN
// From a length const biguint64 = new BigUint64Array(2); biguint64[0] = 42n; console.log(biguint64[0]); // 42n console.log(biguint64.length); // 2 console.log(biguint64.BYTES_PER_ELEMENT); // 8 // From an array const x = new BigUint64Array([21n, 31n]); console.log(x[1]); // 31n // From another TypedArray const y = new BigUint64Array(x); console.log(y[0]); // 21n // From an ArrayBuffer const buffer = new ArrayBuffer(64); const z = new BigUint64Array(buffer, 8, 4); console.log(z.byteOffset); // 8 // From an iterable const iterable = (function* () { yield* [1n, 2n, 3n]; })(); const biguint64FromIterable = new BigUint64Array(iterable); console.log(biguint64FromIterable); // BigUint64Array [1n, 2n, 3n]
Find elsewhere
Top answer
1 of 3
18

JavaScript sort method requires a function as a parameter that can compare two elements of the array and return either a positive number, or a negative number or zero. Number is the keyword here.

BigInt operations like addition and subtraction returns BigInt type and not a Number type. And that's why the error you are getting.

So, Something like this should do the job

const big = [1n, 2n, 3n, 4n];
big.sort((a ,b) => {
  if(a > b) {
    return 1;
  } else if (a < b){
    return -1;
  } else {
    return 0;
  }
});
console.log(big);

Interestingly, MDN document that I linked to previously, also suggests how to sort an array of BigInts, and it is concise:

Copying the whole section here for posterity:

const mixed = [4n, 6, -12n, 10, 4, 0, 0n]
//   [4n, 6, -12n, 10, 4, 0, 0n]

mixed.sort() // default sorting behavior
//   [ -12n, 0, 0n, 10, 4n, 4, 6 ]

mixed.sort((a, b) => a - b)
// won't work since subtraction will not work with mixed types
// TypeError: can't convert BigInt to number

// sort with an appropriate numeric comparator
mixed.sort((a, b) => (a < b) ? -1 : ((a > b) ? 1 : 0))
//   [ -12n, 0, 0n, 4n, 4, 6, 10 ]
2 of 3
4

The reason is that a - b in the sort callback function will return a BigInt data type, while sort expects it to return something that is (or can coerce to) a Number data type.

So you can use a > b || -(a < b) as callback expression:

const big = [10n, 9n, 8n, 7n];
big.sort((a, b) => a > b || -(a < b));
console.log(big + ""); // 7,8,9,10

Note that the first version (without sort callback) does not work in general, because then sort will compare the elements as strings. It is clear that this can yield results that are not numerically sorted:

const big = [10n, 9n, 8n, 7n];
big.sort(); // string-based sort
console.log(big + ""); // 10,7,8,9 is wrong

🌐
GeeksforGeeks
geeksforgeeks.org › bigint-in-javascript
BigInt in JavaScript - GeeksforGeeks
June 25, 2020 - BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger than 253-1. The largest number that JavaScript can reliably represent with the Number primitive is 253-1, which is represented by the MAX_SAFE_INTEGER ...
🌐
GitHub
github.com › tc39 › proposal-bigint
GitHub - tc39/proposal-bigint: Arbitrary precision integers in JavaScript
Arbitrary precision integers in JavaScript. Contribute to tc39/proposal-bigint development by creating an account on GitHub.
Starred by 562 users
Forked by 54 users
Languages   HTML 99.3% | Shell 0.7% | HTML 99.3% | Shell 0.7%
🌐
V8
v8.dev › features › bigint
BigInt: arbitrary-precision integers in JavaScript · V8
BigInts are a new numeric primitive in JavaScript that can represent integers with arbitrary precision. This article walks through some use cases and explains the new functionality in Chrome 67 by comparing BigInts to Numbers in JavaScript.
🌐
GitHub
github.com › juanelas › bigint-conversion
GitHub - juanelas/bigint-conversion: Convert from bigint to buffer (or uint8array), hex string, utf8 string, bas64 and backwards. For both node.js and javascript native.
Convert from bigint to buffer (or uint8array), hex string, utf8 string, bas64 and backwards. For both node.js and javascript native. - juanelas/bigint-conversion
Starred by 14 users
Forked by 4 users
Languages   JavaScript 71.1% | TypeScript 28.9% | JavaScript 71.1% | TypeScript 28.9%
🌐
Smashing Magazine
smashingmagazine.com › 2019 › 07 › essential-guide-javascript-newest-data-type-bigint
The Essential Guide To JavaScript’s Newest Data Type: BigInt — Smashing Magazine
The BigInt data type aims to enable JavaScript programmers to represent integer values larger than the range supported by the Number data type. The ability to represent integers with arbitrary precision is particularly important when performing ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-biguint64array-constructor
JavaScript BigUint64Array() Constructor - GeeksforGeeks
April 28, 2025 - The BigUint64Array() Constructor creates a new typed array of the 64-bit unsigned integers (BigInts). Typed arrays are a way to handle and manipulate binary data in a specific format.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript bigint
JavaScript BigInt
November 14, 2024 - let bigInt = 9007199254740991n;Code language: JavaScript (javascript)
🌐
Quora
quora.com › What-is-BigInt64Array-in-JavaScript
What is BigInt64Array in JavaScript? - Quora
Answer: The [code ]BigInt64Array[/code] typed array represents an array of 64-bit signed integers in the platform byte order. If control over byte order is needed, use DataView instead.