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

Creating BigInt Values

Use the 'n' suffix or BigInt() constructor to create BigInt values from numbers or strings.

Creating BigInt Values

You'll learn two simple ways to create BigInt values in JavaScript to handle integers larger than Number.MAX_SAFE_INTEGER.

The Two Creation Methods

Now that you know why BigInt exists, let's see how to create them. JavaScript gives you two straightforward approaches:

1. The n Suffix

Add an n to the end of an integer literal:

const bigNum = 9007199254740991n;  // This is a BigInt
const huge = 123456789012345678901234567890n;

Think of the n as a flag that tells JavaScript: "Hey, treat this as a BigInt, not a regular Number."

2. The BigInt() Constructor

Pass a number or string to the BigInt() function:

const fromNumber = BigInt(9007199254740991);
const fromString = BigInt("123456789012345678901234567890");

The constructor is especially useful when converting existing values or working with strings that represent very large numbers.

Important Differences

typeof 42n;           // "bigint"
typeof BigInt(42);    // "bigint"
typeof 42;            // "number"

42n === 42;           // false (different types!)
42n == 42;            // true (coerces for comparison)

BigInt values are a separate type from regular numbers. You cannot mix them in arithmetic operations without explicit conversion.

// This works:
const result = 10n + 20n;  // 30n

// This throws an error:
// const bad = 10n + 20;  // TypeError!

Key Takeaway: Create BigInt values by adding n to integer literals or using the BigInt() constructor with numbers or strings—they're a distinct type from regular numbers and cannot be mixed in operations.