JavaScript features a built-in object known as BigInt, which is designed to represent whole numbers that exceed 253-1. In JavaScript, BigInt variables are utilized for storing substantial integer values that cannot be accommodated by standard JavaScript numbers. The BigInt type is a primitive, akin to other primitives such as boolean, undefined, null, string, symbol, and number.
The constant MAXSAFEINTEGER denotes the range from 0 to 2^53 - 1. This value is the maximum integer that JavaScript can reliably represent with the Number primitive type. Understanding this constant is crucial when dealing with extensive datasets, as it has numerous implications for data manipulation and calculations.
Syntax
The syntax below illustrates the bigint data type in JavaScript. There are two approaches to utilize the bigint data type within JavaScript.
- By adding an n at the conclusion of an integer literal.
- Another option is to invoke the function BigInt:
- Parameters: It takes as input a single integer literal that must be represented as a BigInt string.
- Return Type: This method uses the BigInt data type to return the supplied value.
let bigInt_var = 9007199254740991n;
BigInt( numbers )
let bigInt_var = BigInt(9007199254740991);
Examples
The subsequent examples illustrate the usage of a bigint in JavaScript for programming purposes. By utilizing bigint methods, we are able to present and manipulate extensive numerical values.
Example 1:
The subsequent illustration demonstrates the fundamental bigint number in JavaScript by utilizing the "n" suffix. This suffix is appended to the conclusion of the number to denote the bigint data type.
<!DOCTYPE html>
<html>
<head>
<title> BigInt in JavaScript </title>
</head>
<body style="background-color:aqua;">
<h1> BigInt in JavaScript </h1>
<h4> Create a BigInt using the "n" keyword. This keyword is used at the end of the number to represent the bigint data type. </h4>
<p id = "demo1"></p>
<p id = "demo2"></p>
<p id = "demo3"></p>
<script>
let x = 123456789012345678901234567890n;
// Decimal format value
let bigNum_var = 12342222222222222222222222222222222n
console.log(bigNum_var)
// Hexadecimal format value
let bigHex_var = 0x1ffffffeeeeeeeeefn
console.log(bigHex_var)
// Binary format value
let bigBin_var = 0b1010101001010101001111111111111111n
console.log(bigBin_var)
document.getElementById("demo1").innerHTML = "Decimal format BigInt value:" +x + "<br> Decimal format BigInt value:" + bigNum_var;
document.getElementById("demo2").innerHTML = "Hexadecimal format BigInt value: " + bigHex_var;
document.getElementById("demo3").innerHTML = "Binary format BigInt value: " + bigBin_var;
</script>
</body>
</html>
Output
The output shows the bigint value in JavaScript.
Example 2:
The subsequent example illustrates the fundamental bigint number in JavaScript through the utilization of the "BigInt" method. This method is employed with the numerical input placed within the parentheses to denote the bigint data type.
<!DOCTYPE html>
<html>
<head>
<title> BigInt in JavaScript </title>
</head>
<body style="background-color:aqua;">
<h2> BigInt in JavaScript </h2>
<h4> Create a BigInt using using the "BigInt" method. This method uses with the input number in the bracket to represent the bigint data type. </h4>
<p id = "demo1"></p>
<p id = "demo2"></p>
<p id = "demo3"></p>
<script>
let x = BigInt(12345678901234567890123456121110);
// Decimal format value
let bigNum_var = BigInt(
"12342222228888888888888222222255");
console.log(bigNum_var)
// Hexadecimal format value
let bigHex_var = BigInt(0x1ffffffeeeeeeeeed);
console.log(bigHex_var)
// Binary format value
let bigBin_var = BigInt(0b1010101001010101001111001111111111);
console.log(bigBin_var)
document.getElementById("demo1").innerHTML = "Decimal format BigInt value:" +x + "<br> Decimal format BigInt value:" + bigNum_var;
document.getElementById("demo2").innerHTML = "Hexadecimal format BigInt value: " + bigHex_var;
document.getElementById("demo3").innerHTML = "Binary format BigInt value: " + bigBin_var;
</script>
</body>
</html>
Output
The output shows the bigint value in JavaScript.
Example 3:
The subsequent example illustrates the data type of a value by utilizing the typeof operator. It demonstrates that the data type of the value is bigint.
<!DOCTYPE html>
<html>
<head>
<title> BigInt in JavaScript </title>
</head>
<body style="background-color:aqua;">
<h2> BigInt in JavaScript </h2>
<h4> Create a BigInt using using the "BigInt" method. We can see the data type of the value. </h4>
<p id = "demo1"></p>
<p id = "demo2"></p>
<p id = "demo3"></p>
<script>
let x = 110n;
// Decimal format value
let bigNum_var = BigInt(
"12342222228888888888888222222255");
// Hexadecimal format value
let bigHex_var = 0x1ffffffeeeeeeeeedn;
// Binary format value
let bigBin_var = BigInt(0b1010101001010101001111001111111111);
document.getElementById("demo1").innerHTML = "Decimal format:" + typeof x + "<br> Decimal format data type:" + typeof bigNum_var;
document.getElementById("demo2").innerHTML = "Hexadecimal format data type: " + typeof bigHex_var;
document.getElementById("demo3").innerHTML = "Binary format data type: " + typeof bigBin_var;
</script>
</body>
</html>
Output
The output shows the bigint value in JavaScript.
Example 4:
The subsequent example demonstrates the functionality of the addition, subtraction, and division operators. In this instance, we will utilize decimal, binary, octal, and hexadecimal numeral systems for the respective operations.
<!DOCTYPE html>
<html>
<head>
<title> BigInt in JavaScript </title>
</head>
<body style="background-color:aqua;">
<h2> BigInt in JavaScript </h2>
<h4> We can operate the bigint number using an operator with different formats and other types of values. </h4>
<p id = "demo1"></p>
<p id = "demo2"></p>
<p id = "demo3"></p>
<p id = "demo4"></p>
<script>
let x = BigInt(12345678901234567890123456121110);
// Decimal format value
let x1 = BigInt(
"12342222228888888888888222222255");
let bigNum_var = x + x1;
console.log(bigNum_var)
// Hexadecimal format value
let y = BigInt(0x1ffffffeeeeeeeeed);
let y1 = BigInt(0x1ffffffeeed);
let bigHex_var = y - y1;
console.log(bigHex_var)
// Binary format value
let z = BigInt(0b1010101001010101001111001111111111);
let z1 = BigInt(0b101010100101010100111100);
let bigBin_var = z/z1;
console.log(bigBin_var);
// octal format value
let u = BigInt(0o400000000000000003);
let u1 = BigInt(0o2000000000000001);
let bigOct_var = u * u1;
console.log(bigBin_var);
document.getElementById("demo1").innerHTML = "Decimal format with addition operator: " + bigNum_var;
document.getElementById("demo2").innerHTML = "Hexadecimal format with subtraction operator: " + bigHex_var;
document.getElementById("demo3").innerHTML = "Binary format with division operator: " + bigBin_var;
document.getElementById("demo4").innerHTML = "Octal format with multiplication operator: " + bigOct_var;
</script>
</body>
</html>
Output
The output shows the bigint value in JavaScript.
Example 5:
The subsequent example demonstrates the utilization of comparison operators for "equal to," "greater than," and "less than" operations. We will employ decimal, binary, octal, and hexadecimal number systems for the purpose of comparison.
<!DOCTYPE html>
<html>
<head>
<title> BigInt in JavaScript </title>
</head>
<body style="background-color:aqua;">
<h2> BigInt in JavaScript </h2>
<h4> We can operate the bigint number using a comparison operator with different formats of the value. </h4>
<p id = "demo1"></p>
<p id = "demo2"></p>
<p id = "demo3"></p>
<p id = "demo4"></p>
<script>
let x = BigInt(12345678901234567890123456121110);
// Decimal format value
let x1 = BigInt(
"12342222228888888888888222222255");
let bigNum_var = x === x1;
console.log(bigNum_var)
// Hexadecimal format value
let y = BigInt(0x1ffffffeeeeeeeeed);
let y1 = BigInt(0x1ffffffeeed);
let bigHex_var = y >= y1;
console.log(bigHex_var)
// Binary format value
let z = BigInt(0b1010101001010101001111001111111111);
let z1 = BigInt(0b101010100101010100111100);
let bigBin_var = z1 < z;
console.log(bigBin_var);
// octal format value
let u = BigInt(0o400000000000000003);
let u1 = BigInt(0o400000000000000003);
let bigOct_var = u === u1;
console.log(bigBin_var);
document.getElementById("demo1").innerHTML = "Decimal format with equalTo operator: " + bigNum_var;
document.getElementById("demo2").innerHTML = "Hexadecimal format greater than operator: " + bigHex_var;
document.getElementById("demo3").innerHTML = "Binary format with less than operator: " + bigBin_var;
document.getElementById("demo4").innerHTML = "Octal format with equalTo operator: " + bigOct_var;
</script>
</body>
</html>
Output
The output shows the bigint value in JavaScript.
Example 6:
The illustration demonstrates both the highest and lowest secure values that can be represented by the BigInt data type.
<!DOCTYPE html>
<html>
<head>
<title> BigInt in JavaScript </title>
</head>
<body style="background-color:aqua;">
<h2> BigInt in JavaScript </h2>
<h4> We can see the Minimum and Maximum Safe Integers. We can see the data type of the value. </h4>
<p id = "demo1"></p>
<p id = "demo2"></p>
<script>
// maxsafe for the bigint
let bigNum_var = Number.MAX_SAFE_INTEGER;
// minsafe for the bigint
let bigMin_var = Number.MIN_SAFE_INTEGER;
// Binary format value
let bigBin_var = BigInt(0b1010101001010101001111001111111111);
document.getElementById("demo1").innerHTML = "max safe number: " + bigNum_var + "<br> data type of the value:" + typeof bigNum_var;
document.getElementById("demo2").innerHTML = "Min safe number: " + bigMin_var + "<br> data type of the value:" + typeof bigMin_var;
</script>
</body>
</html>
Output
The output shows the bigint value in JavaScript.
Example 7:
The illustration demonstrates the array utilized for the operation. In JavaScript, we are able to sort using both integers and bigint numbers.
<!DOCTYPE html>
<html>
<head>
<title> BigInt in JavaScript </title>
</head>
<body style="background-color:aqua;">
<h2> BigInt in JavaScript </h2>
<h4> We can see the area with int and bigInt numbers. <br> The array value is sorted by ascending order. </h4>
<p id = "demo1"></p>
<p id = "demo2"></p>
<script>
// Array consisting of both
// Number and BigInt
let arr_var = [4, 2, 5n, 2n, 3, 6, 6n, 7n, 1]
document.getElementById("demo1").innerHTML = "Array value before sorting: " + arr_var;
// Sorting the array of the variable
arr_var.sort()
console.log(arr_var);
document.getElementById("demo2").innerHTML = "Array value after sorting: " + arr_var + "<br> data type of the value:" + typeof arr_var;
</script>
</body>
</html>
Output
The output shows the bigint value in JavaScript.
Supported Browsers:
The following browsers are supported by the BigInt method in javascript:
- Chrome
- Edge
- Firefox
- Opera
- Safari
Conclusion
The bigint represents a fundamental data type designed to manage and manipulate large numerical values.