![]() |
VOOZH | about |
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.
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.
123422222222222222222222222222222222222n 36893488074118328047n 11430854655n
BigInt( number ) orAppending n to end of an integer literalIt accepts a single integer literal as a string that needs to be represented as BigInt.
This method returns the given value as BigInt data type.
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.
123422222222222222222222222222222222222n 36893488074118328047n 11430854655n
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 false
typeof 100n === 'bigint' // Returns true
100n < 101 // Returns true due to coercion
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].
[ 2, 2n, 4, 5n ]
The following applications are not recommended to be used with BigInt due to its implementation:
BigInt or neither).BigInt and regular numbers (precision may be lost).>>>) is not supported for BigInt.The browsers supporting BigInt method are listed below: