JavaScript is a popular programming language that is used to create dynamic and interactive web pages. It is a versatile language that can be used for a wide range of applications, from simple scripts to complex web applications. One of the key features of JavaScript is its ability to handle numbers. However, JavaScript has a limitation when it comes to handling large numbers. This is where JS BigInt comes in.
JS BigInt is a built-in object in JavaScript that allows you to work with integers of arbitrary length. It was introduced in ECMAScript 2020 and is designed to handle large numbers that are beyond the range of the Number data type in JavaScript. The Number data type in JavaScript can only handle numbers up to 2^53 - 1. Any number larger than this will result in a loss of precision. JS BigInt, on the other hand, can handle numbers of any size without losing precision.
JS BigInt is represented by the BigInt data type. To create a BigInt, you simply add the letter "n" to the end of an integer. For example:
<script>
let bigNum = 1234567890123456789012345678901234567890n;
console.log(bigNum);
</script>
In the above example, we have created a BigInt with the value of 1234567890123456789012345678901234567890. Note the "n" at the end of the number, which indicates that it is a BigInt.
JS BigInt supports all the basic arithmetic operations, such as addition, subtraction, multiplication, and division. Here are some examples:
<script>
let num1 = 1234567890123456789012345678901234567890n;
let num2 = 9876543210987654321098765432109876543210n;
console.log(num1 + num2);
console.log(num1 - num2);
console.log(num1 * num2);
console.log(num1 / num2);
</script>
In the above example, we have created two BigInts and performed various arithmetic operations on them. Note that the result of each operation is also a BigInt.
JS BigInt also supports comparison operators, such as greater than, less than, and equal to. Here are some examples:
<script>
let num1 = 1234567890123456789012345678901234567890n;
let num2 = 9876543210987654321098765432109876543210n;
console.log(num1 > num2);
console.log(num1 < num2);
console.log(num1 === num2);
</script>
In the above example, we have compared two BigInts using the greater than, less than, and equal to operators.
JS BigInt also supports bitwise operators, such as bitwise AND, bitwise OR, and bitwise XOR. Here are some examples:
<script>
let num1 = 1234567890123456789012345678901234567890n;
let num2 = 9876543210987654321098765432109876543210n;
console.log(num1 & num2);
console.log(num1 | num2);
console.log(num1 ^ num2);
</script>
In the above example, we have performed various bitwise operations on two BigInts.
JS BigInt is a powerful feature in JavaScript that allows you to work with large numbers without losing precision. It is a useful tool for developers who need to work with numbers that are beyond the range of the Number data type in JavaScript. With JS BigInt, you can perform all the basic arithmetic operations, comparison operations, and bitwise operations on large numbers.