JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS BigInt


📘 JavaScript BigInt – Handling Very Large Integers

The BigInt type was introduced in ES2020 to safely represent integers larger than 2^53 - 1, which is the limit of the Number type in JavaScript.


🔹 Why BigInt?

JavaScript’s regular Number type uses 64-bit floating-point representation, which cannot accurately represent integers larger than 2⁵³ - 1 (9007199254740991).

console.log(Number.MAX_SAFE_INTEGER);  // 9007199254740991
console.log(9007199254740991 + 1);     // 9007199254740992 ✅
console.log(9007199254740991 + 2);     // 9007199254740992 ❌ (wrong!)

🔹 Creating BigInt

✅ Using n suffix:
let big = 1234567890123456789012345678901234567890n;
✅ Using BigInt() constructor:
let big2 = BigInt("1234567890123456789012345678901234567890");

🔹 BigInt Operations

let a = 10n;
let b = 20n;

let sum = a + b;        // 30n
let diff = b - a;       // 10n
let prod = a * b;       // 200n
let div = b / a;        // 2n (no decimal!)
let mod = b % a;        // 0n

📌 Note: Division always returns the integer part (no decimal points).


🔹 BigInt vs Number – No Mixing!

let big = 10n;
let num = 10;

console.log(big + num);  // ❌ TypeError: Cannot mix BigInt and other types

✅ You must explicitly convert:

let total = big + BigInt(num);

🔹 Comparisons

10n === 10       // false (different types)
10n == 10        // true  (values are equal)

🔹 typeof BigInt

typeof 10n;      // "bigint"

Practice Questions

Q1. How do you declare a BigInt value 12345678901234567890123n and store it in a variable named largeNumber?

Q2. How do you create a BigInt using the BigInt() function with a string value?

Q3. What is the output of adding two BigInts 100n and 200n?

Q4. How do you subtract 10n from 25n and store the result?

Q5. How do you divide 25n by 4n and what is the result?

Q6. How do you convert a regular number 100 into a BigInt before adding to 1000n?

Q7. Why does mixing BigInt and Number directly in arithmetic cause a TypeError?

Q8. How do you find the type of a BigInt variable using typeof?

Q9. How do you check whether 10n == 10 returns true or false and why?

Q10. How do you find the remainder when dividing 55n by 6n using the % operator?


JS BigInt Quiz

Q1: What is the type returned by typeof 123456789n?

A. "number"
B. "big"
C. "BigInt"
D. "bigint"

Q2: Which of the following is a valid way to declare a BigInt?

A. let x = 123n;
B. let x = BigInt("123");
C. Both a and b
D. None of the above

Q3: What will be the result of 10n + 20?

A. 30n
B. 30
C. TypeError
D. undefined

Q4: What does BigInt("9999999999999999999999") return?

A. Error
B. NaN
C. A BigInt
D. Infinity

Q5: What is the output of 25n / 4n?

A. 6.25n
B. 6n
C. 7n
D. 5n

Q6: Which of the following operations is invalid with BigInt?

A. Addition
B. Subtraction
C. Mixing with Number
D. Modulus

Q7: What will 10n == 10 return?

A. false
B. true
C. TypeError
D. undefined

Q8: What will 10n === 10 return?

A. true
B. false
C. TypeError
D. undefined

Q9: Which method can be used to convert 100 (Number) into BigInt?

A. parseInt()
B. BigInt()
C. String()
D. toBigInt()

Q10: What will be the result of typeof BigInt("100")?

A. "BigInt"
B. "number"
C. "bigint"
D. "object"

Go Back Top