BigInt in JavaScript - GeeksforGeeks

25 Jun, 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 constant. This has various uses where operations on large numbers are required.
Syntax:
BigInt( number ) 
or
Appending n to end of an integer literal

Parameters: It accepts a single integer literal as string that needs to represent as BigInt.
Return Type: This method returns the given value as BigInt data type.
Example: This example creating a BigInt using the BigInt() function.
javascript
// Parameter in decimal formatvar bigNum = BigInt(  "123422222222222222222222222222222222222");console.log(bigNum);  // Parameter in hexadecimal formatvar bigHex = BigInt("0x1ffffffeeeeeeeeef");console.log(bigHex);  // Parameter in binary formatvar bigBin = BigInt(  "0b1010101001010101001111111111111111");console.log(bigBin);
Output: 
123422222222222222222222222222222222222n
36893488074118328047n
11430854655n

Example: This example creating a BigInt by appending n at the end of the number.
javascript
// Decimal formatvar bigNum = 123422222222222222222222222222222222222nconsole.log(bigNum)  // Hexadecimal formatvar bigHex = 0x1ffffffeeeeeeeeefnconsole.log(bigHex)  // Binary formatvar bigBin = 0b1010101001010101001111111111111111nconsole.log(bigBin)
Output: 
123422222222222222222222222222222222222n
36893488074118328047n
11430854655n

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:
javascript
// Array consisting of both// Number and BigIntvar arr = [4, 2, 5n, 2n]  // Sorting the arrayarr.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.
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.
Supported Browsers: The browsers supporting BigInt method are listed below:
Chrome
Edge
Firefox
Opera
Data Types
javascript-basics
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript