JavaScript BigInt



JavaScript BigInt

  • JavaScript BigInt variables are used to store big integer values that are too big to be represented by a normal JavaScript Number.

JavaScript Integer Accuracy

  • JavaScript integers are only accurate up to 15 digits.
javascript-bigint

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>
<h2>Integer Precision</h2>

<p>Integers 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>

Output

JavaScript Numbers
Integer Precision
Integers are accurate up to 15 digits:

999999999999999
10000000000000000
  • In JavaScript, all numbers are stored in a 64-bit floating-point format (IEEE 754 standard).
  • With this standard, large integer cannot be exactly represented and will be rounded.
  • Because of this, JavaScript can only safely represent integers.
  • Up to 9007199254740991 +(253-1) and Down to -9007199254740991 -(253-1).
  • Integer values outside this range lose precision.

How to Create a BigInt

  • To create a BigInt, append n to the end of an integer or call BigInt().
javascript-bigint-typeof

Sample Code

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Numbers</h1>
<h2>Integer and BigInt</h2>

<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
let x = 9999999999999999;
let y = BigInt("9999999999999999");
document.getElementById("demo").innerHTML = x + "<br>" + y;
document.getElementById("demo1").innerHTML ="type of x "+typeof x;
document.getElementById("demo2").innerHTML ="type of y "+typeof y;
</script>

</body>
</html>

Output

JavaScript Numbers
Integer and BigInt
10000000000000000
9999999999999999

type of x number

type of y bigint

BigInt is the second numeric data type in JavaScript (after Number).

  • With BigInt the total number of supported data types in JavaScript is 8.
    • String
    • Number
    • Bigint
    • Boolean
    • Undefined
    • Null
    • Symbol
    • Object

Related Searches to JavaScript BigInt