Working With BigInt In JavaScript: Hello everyone, welcome to the nkcoderz.com website. In this article we will going to discuss about the Working With BigInt In JavaScript.
Working With BigInt In JavaScript
JavaScript’s built-in number data type is limited to 53-bit precision, which means that it can only accurately represent integers between -9007199254740991 and 9007199254740991.
This is often not sufficient when dealing with large integers, such as those used in cryptography or financial applications.
To handle these large integers, JavaScript introduced the BigInt data type in ECMAScript 2020. You can create a BigInt value by appending an n
to the end of a number literal, like this:
Code For BigInt In JavaScript
const bigIntValue = 1234567890123456789012345678901234567890n;
You can also convert a number to a BigInt
using the BigInt()
function:
const numberValue = 1234567890123456789012345678901234567890;
const bigIntValue = BigInt(numberValue);
Once you have a BigInt
value, you can perform all the basic arithmetic operations on it, such as addition, subtraction, multiplication, and division:
const x = 1234567890123456789012345678901234567890n;
const y = 98765432109876543210987654321098765432109n;
const sum = x + y;
const difference = x - y;
const product = x * y;
const quotient = x / y;
Explanation
Note that BigInt
values are not interchangeable with regular JavaScript numbers. You need to use the n
suffix or the BigInt()
function when creating or converting a BigInt
value, and you cannot perform arithmetic operations between a BigInt
and a regular number without converting one of them first.
In addition, BigInt
values have a separate set of comparison operators, such as <
, >
, <=
, >=
, and ==
. Regular comparison operators do not work with BigInt
values.
Conclusion
If you liked this post Working With BigInt In JavaScript, then please share this with your friends and make sure to bookmark this website for more awesome content.