Open In App

How to Deal with Large Numbers in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

Large numbers are the numbers that can hold huge memory and evaluation time is more than exceeds space and time to process. We can deal with large numbers in JavaScript using the data type BigInt.

Advantages:

  • It can hold numbers of large sizes.
  • It performs arithmetic operations.

Disadvantages:

  • Consumes huge memory.

Approach

By default, JavaScript converts a big number by adding e+39 at the end of it.

let variable_name = value
This will print e+39 at last
let bigi = 41234563232789012327892787227897329;
Output: 4.123456323278901e+34

So to remove this, add ‘n’ at the end of the number

let bigi = 41234563232789012327892787227897329n;
output: 41234563232789012327892787227897329
They are used in numerical calculations used along with operands

Example 1: This example shows the use of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redirect Example</title>
</head>
 
<body>
    <h1 style="text-align:center;color:green">
        GeeksforGeeks
    </h1>
 
    <p id="gfg1"></p>
 
    <p id="gfg2"></p>
 
    <script>
        let bigit = 41234563232789012327892787227897329;
        document.getElementById("gfg1").innerHTML
            = "The value of bigit is: " + bigit;
 
        // Displaying full number
        let bigit1 = 41234563232789012327892787227897329n;
        document.getElementById("gfg2").innerHTML
            = "The value of bigit1 is: " + bigit1;
    </script>
</body>
 
</html>


Output:

Deal with Large Numbers

Example 2: This example shows the use of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redirect Example</title>
</head>
 
<body>
    <h1 style="text-align:center;color:green">
        GeeksforGeeks
    </h1>
 
 
    <p id="gfg2"></p>
    <p id="gfg3"></p>
    <p id="gfg4"></p>
    <p id="gfg5"></p>
    <p id="gfg6"></p>
    <p id="gfg7"></p>
 
    <script>
        let bigit1 = 41234563232789012327892787227897329n;
        document.getElementById("gfg2").innerHTML
            = "The value of bigit1 is: " + bigit1;
        // The value of bigit1 is:
        // 41234563232789012327892787227897329
 
        // The value of bigi is:
        // 71234563232789012327892787227897329
        let bigi = 71234563232789012327892787227897329n;
        document.getElementById("gfg3").innerHTML
            = "The value of bigi is: " + bigi;
 
 
        // Addition
        let z = bigit1 + bigi
        document.getElementById("gfg4").innerHTML
            = "The Addition result is: " + z;
        // The Addition result is:
        // 112469126465578024655785574455794658
 
 
        //subtraction
        let a = bigit1 - bigi
        document.getElementById("gfg5").innerHTML
            = "The subtraction result is: " + a;
        // The subtraction result is:
        // -30000000000000000000000000000000000
 
        // Multiplication
        let b = bigit1 * bigi
        document.getElementById("gfg6").innerHTML
            = "The multiplication result is: " + b;
        // The multiplication result is:
        // 293732610198254581311205146182139547
        // 9295010026777045763269038565334241
 
        // Division
        let c = bigit1 / bigi
        document.getElementById("gfg7").innerHTML
            = "The division result is: " + c;
        // The division result is: 0
    </script>
</body>
 
</html>


Output:

Deal with Large Numbers

Other uses of Large Numbers:

1. Use BigInt for Integer Arithmetic (ES6 and later):

JavaScript introduced the BigInt type to handle integers of arbitrary precision. It is denoted by appending n to the end of a numeric literal or by using the BigInt() constructor.

const largeNumber = 1234567890123456789012345678901234567890n;

2. Use Libraries for Arbitrary Precision Arithmetic:

Consider using external libraries like BigNumber.js or Decimal.js for arbitrary precision arithmetic.

Example using BigNumber.js:

// Include the library in your HTML file
// <script src="https://cdn.jsdelivr.net/npm/bignumber.js@10"></script>
// Usage in your JavaScript
const BigNumber = require('bignumber.js');
const largeNumber = new BigNumber('1234567890123456789012345678901234567890');

3. String Representation:

For very large numbers, consider representing them as strings to avoid precision issues.

const largeNumberAsString = '1234567890123456789012345678901234567890';

4. Scientific Notation:

Use scientific notation for very large or very small numbers.

const veryLargeNumber = 1.23e30;
const verySmallNumber = 1.23e-30;

5. Avoid Direct Equality Comparisons:

Due to floating-point precision, avoid direct equality comparisons for large numbers. Instead, use a threshold for comparison.

const threshold = 1e-10;
if (Math.abs(number1 - number2) < threshold) {
// Numbers are considered equal within the threshold
}

6. Consider Workarounds:

If precision is critical and you are facing issues with standard arithmetic operations, consider alternative approaches or algorithms that reduce precision errors.

7. Use Exponentiation Operator (ES6 and later):

For large integers, you can use the exponentiation operator (**) to handle exponentiation more efficiently.

const largeResult = 2n ** 64n; // 2 to the power of 64


Last Updated : 19 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads