Course contentsShow
← JavaScript
Lesson 1971 of 2,72341. Modern JavaScript Features (ES2020+)Free lesson

Comparisons and Equality with BigInt

Compare BigInt values with each other and understand how equality works across types.

Comparisons and Equality with BigInt

You'll learn: How to compare BigInt values and understand the subtle differences between strict and loose equality when BigInt meets Number.

The Core Idea

BigInt values can be compared with each other using the standard comparison operators (<, >, <=, >=), and they work exactly as you'd expect. However, equality is where things get interesting: BigInt and Number are different types, so strict equality (===) will always return false when comparing across types, but loose equality (==) will compare their mathematical values.

Think of it like comparing measurements in different units: 1 meter and 100 centimeters are "equal" in value but not identical in representation.

Comparing BigInt Values

const big1 = 100n;
const big2 = 200n;

console.log(big1 < big2);   // true
console.log(big1 > big2);   // false
console.log(big1 <= 100n);  // true
console.log(big1 >= 100n);  // true

BigInt vs Number Equality

const bigInt = 42n;
const regular = 42;

console.log(bigInt == regular);   // true (loose equality compares value)
console.log(bigInt === regular);  // false (different types)
console.log(typeof bigInt);       // "bigint"
console.log(typeof regular);      // "number"

Mixed Comparisons Work

console.log(10n < 20);    // true
console.log(5n > 3);      // true
console.log(100n == 100); // true
console.log(100n === 100); // false

Comparison operators (<, >, etc.) compare the mathematical values even across types, but identity matters for ===.

Key Takeaway: BigInt values compare naturally with each other and loosely (==) with Numbers based on value, but strict equality (===) requires both type and value to match, so 42n === 42 is always false.