Open In App

JavaScript BigInt

Last Updated : 15 Jul, 2025
Suggest changes
Share
Like Article
Like
Report

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 or string value. It allows precise arithmetic with integers beyond the safe integer limit of regular numbers.

1. Creating BigInt using BigInt() Function

Example: In this example we creates BigInt numbers in decimal, hexadecimal, and binary formats, then prints each using console.log. It demonstrates different ways to represent large integers.

JavaScript
// Parameter in decimal format let bigNum = BigInt(  "123422222222222222222222222222222222222"); console.log(bigNum); // Parameter in hexadecimal format let bigHex = BigInt("0x1ffffffeeeeeeeeef"); console.log(bigHex); // Parameter in binary format let bigBin = BigInt(  "0b1010101001010101001111111111111111"); console.log(bigBin); 

Output
123422222222222222222222222222222222222n 36893488074118328047n 11430854655n 

Syntax

BigInt( number ) orAppending n to end of an integer literal

Parameters

It accepts a single integer literal as a string that needs to be represented as BigInt.

Return Type

This method returns the given value as BigInt data type.

2. Creating BigInt by appending n

Example: In this example we creates BigInt numbers directly in decimal, hexadecimal, and binary formats, then prints each using console.log. It demonstrates various BigInt literals.

JavaScript
// Decimal format let bigNum = 123422222222222222222222222222222222222n console.log(bigNum) // Hexadecimal format let bigHex = 0x1ffffffeeeeeeeeefn console.log(bigHex) // Binary format let bigBin = 0b1010101001010101001111111111111111n console.log(bigBin) 

Output
123422222222222222222222222222222222222n 36893488074118328047n 11430854655n 

3. Comparing BigInt other types

A BigInt is similar to a Number in some ways, however, it cannot be used with methods of the builtin Math object and cannot be mixed with instances of Number in operations.

Example: Comparing BigInt with a Number.

typeof 100n === 100 // Returns false typeof 100n == 100 // Returns true due to coercion typeof 100n === 'bigint' // Returns true 100n < 101 // Returns true due to coercion 

Sorting

An array can hold both primitive data types and BigInts. This allows the sort() method to work when both normal Number and BigInt values are present in the array.

Example: In this example we creates an array with both Number and BigInt types, sorts it using arr.sort(), and prints the sorted array, which will be [2, 2n, 4, 5n].

JavaScript
// Array consisting of both // Number and BigInt let arr = [4, 2, 5n, 2n] // Sorting the array arr.sort() console.log(arr) // [2, 2n, 4, 5n] 

Output
[ 2, 2n, 4, 5n ] 

Usage Recommendation

The following applications are not recommended to be used with BigInt due to its implementation:

  • Coercion: Coercing between Number and BigInt can lead to loss of precision, it is recommended to only use BigInt when values greater than 253 are reasonably expected and not to coerce between the two types.
  • Cryptography: The operations supported on BigInt are not constant time. BigInt is therefore unsuitable for use in cryptography.

Limitations and Considerations

  • Some operators don’t support mixed types (both operands must be BigInt or neither).
  • Be cautious when coercing between BigInt and regular numbers (precision may be lost).
  • Unsigned right shift (>>>) is not supported for BigInt.

Supported Browsers

The browsers supporting BigInt method are listed below:


Explore