JavaScript BigInt
JavaScript Integer Accuracy
JavaScript integers are only accurate up to 15 digits:
In JavaScript, all numbers are kept in a particular way called '64-bit floating-point format' based on the IEEE 754 standard.
This rule means that really big numbers can't be shown exactly and will be approximated.
Because of this, JavaScript can only safely represent integers:
Maximum: 9,007,199,254,740,991 + (9,007,199,254,740,991 - 1)
and
Decreasing to a very large negative number, which is equivalent to -9007199254740991, and it is specifically one less than two to the power of 53.
Integer values outside this range lose precision.
How to Create a BigInt
To make a **BigInt**, add 'n' at the end of a whole number, or use the function **BigInt()**.
BigInt: A new JavaScript Datatype
In JavaScript, when you use the typeof
operator on a variable that contains a BigInt
, it will return "bigint".
In JavaScript, we have another numeric data type called BigInt
. It comes right after the "Number" data type.
JavaScript can handle 8 different data types, including BigInt
.
1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object
BigInt Operators
You can use the same operations on a JavaScript Number as you can on a BigInt.
Notes : You can't perform arithmetic directly between a BigInt and a Number because it may lead to loss of information due to type conversion.
Unsigned right shift (>>>) can not be done on a BigInt
(it does not have a fixed width).
BigInt Decimals
A BigInt
doesn't support decimal numbers.
BigInt Hex, Octal and Binary
BigInt
can also be written in hexadecimal, octal, or binary notation:
Precision Curiosity
Rounding can make a program less secure.
Minimum and Maximum Safe Integers
ES6 added max and min properties to the Number object:
MAX_SAFE_INTEGER
MIN_SAFE_INTEGER
New Number Methods
ES6 also added 2 new methods to the Number object:
Number.isInteger()
Number.isSafeInteger()
The Number.isInteger() Method
The Number.isInteger()
function checks if the given value is a whole number and returns true
if it is.
The Number.isSafeInteger() Method
A safe integer is a whole number that can be precisely expressed as a double precision number.
The Number.isSafeInteger()
function tells us if a number is safe or not. If it's a safe number, it returns true
.
Safe integers are all whole numbers from -(2^53 - 1) to +(2^53 - 1).
This is safe: 9007199254740991. This is not safe: 9007199254740992.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>Integer Precision</h2>
<p>Integers (numbers without a period or exponent notation) are accurate up to 15 digits:</p>
<p id="demo"></p>
<script>
let x = 999999999999999;
let y = 9999999999999999;
document.getElementById("demo").innerHTML = x + " <br> " + y;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>Integer and BigInt</h2>
<p id="demo"></p>
<script>
let x = 9999999999999999;
let y = BigInt("9999999999999999");
document.getElementById("demo").innerHTML = x + " <br> " + y;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>BigInt typeof</h2>
<p>The typeof a BigInt is:</p>
<p id="demo"></p>
<script>
let x = BigInt("9999999999999999");
document.getElementById("demo").innerHTML = typeof x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavScript Bigint</h1>
<h2>BigInt Multiply</h2>
<p>Operators that work with regular numbers also work with BigInts.</p>
<p id="demo"></p>
<script>
let x = 9007199254740995n;
let y = 9007199254740995n;
let z = x * y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavScript Bigint</h1>
<h2>BigInt Divide</h2>
<p>A BigInt can not have decimals.</p>
<p>You can't mix really big numbers with other types. You need to convert them explicitly.</p>
<p id="demo"></p>
<script>
let x = 5n;
let y = Number(x) / 2;
document.getElementById("demo").innerHTML = y;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavScript Bigint</h1>
<h2>Hex, Octal and Binary</h2>
<p id="demo"></p>
<script>
let hex = 0x20000000000003n;
let oct = 0o400000000000000003n;
let bin = 0b100000000000000000000000000000000000000000000000000011n;
document.getElementById("demo").innerHTML = hex + " <br> " + oct + " <br> " + bin;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavScript Numbers</h1>
<h2>Integer Precision</h2>
<p>Is 9007199254740992 equal to 9007199254740993?</p>
<p id="demo"></p>
<script>
let x = 9007199254740992 === 9007199254740993;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>Number Object Properties</h2>
<p>MAX_SAFE_INTEGER</p>
<p id="demo"></p>
<script>
let x = Number.MAX_SAFE_INTEGER;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>Number Object Properties</h2>
<p>MIN_SAFE_INTEGER</p>
<p id="demo"></p>
<script>
let x = Number.MIN_SAFE_INTEGER;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>The isInteger() Method</h2>
<p>The isInteger() method returns true if the argument is an integer.</p>
<p>Otherwise it returns false.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Number.isInteger(10) + " <br> " + Number.isInteger(10.5);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Numbers</h1>
<h2>The isSafeInteger() Method</h2>
<p>The isSafeInteger() method returns true if the argument is a safe integer.</p>
<p>Otherwise it returns false.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = Number.isSafeInteger(10) + " <br> " + Number.isSafeInteger(12345678901234567890);
</script>
</body>
</html>