Lesson guide

Learning objectives

  • Explain the main purpose of BigInt in JavaScript.
  • Identify the syntax, APIs, or concepts introduced in this lesson.
  • Use the examples to predict how JavaScript will behave before you run similar code.
  • Connect this topic to nearby lessons in Miscellaneous.

Real-world context

This lesson matters when you need to recognize where BigInt fits into real JavaScript programs. A recent.

Key ideas

  • BigInt is part of the Miscellaneous chapter, so it builds on the surrounding concepts rather than standing alone.
  • Read each code example in two passes: first for the result, then for the rule that explains the result.
  • When a section compares similar features, focus on the condition that makes you choose one feature over another.

Key terms

  • BigInt
  • Miscellaneous
  • JavaScript

A recent addition

This is a recent addition to the language. You can find the current state of support at https://caniuse.com/#feat=bigint.

BigInt is a special numeric type that provides support for integers of arbitrary length.

A bigint is created by appending n to the end of an integer literal or by calling the function BigInt that creates bigints from strings, numbers etc.

javascript
const bigint = 1234567890123456789012345678901234567890n;

const sameBigint = BigInt("1234567890123456789012345678901234567890");

const bigintFromNumber = BigInt(10); // same as 10n

Math operators

BigInt can mostly be used like a regular number, for example:

javascript
alert(1n + 2n);

alert(5n / 2n);

Please note: the division 5/2 returns the result rounded towards zero, without the decimal part. All operations on bigints return bigints.

We can’t mix bigints and regular numbers:

javascript
alert(1n + 2); // Error: Cannot mix BigInt and other types

We should explicitly convert them if needed: using either BigInt() or Number(), like this:

javascript
let bigint = 1n;
let number = 2;

// number to bigint
alert(bigint + BigInt(number)); // 3

// bigint to number
alert(Number(bigint) + number); // 3

The conversion operations are always silent, never give errors, but if the bigint is too huge and won’t fit the number type, then extra bits will be cut off, so we should be careful doing such conversion.

The unary plus is not supported on bigints

The unary plus operator +value is a well-known way to convert value to a number.

In order to avoid confusion, it’s not supported on bigints:

javascript
let bigint = 1n;

alert( +bigint ); // error

So we should use Number() to convert a bigint to a number.

Comparisons

Comparisons, such as <, > work with bigints and numbers just fine:

javascript
alert( 2n > 1n ); // true

alert( 2n > 1 ); // true

Please note though, as numbers and bigints belong to different types, they can be equal ==, but not strictly equal ===:

javascript
alert( 1 == 1n ); // true

alert( 1 === 1n ); // false

Boolean operations

When inside if or other boolean operations, bigints behave like numbers.

For instance, in if, bigint 0n is falsy, other values are truthy:

javascript
if (0n) {
  // never executes
}

Boolean operators, such as ||, && and others also work with bigints similar to numbers:

javascript
alert( 1n || 2 ); // 1 (1n is considered truthy)

alert( 0n || 2 ); // 2 (0n is considered falsy)

Polyfills

Polyfilling bigints is tricky. The reason is that many JavaScript operators, such as +, - and so on behave differently with bigints compared to regular numbers.

For example, division of bigints always returns a bigint (rounded if necessary).

To emulate such behavior, a polyfill would need to analyze the code and replace all such operators with its functions. But doing so is cumbersome and would cost a lot of performance.

So, there’s no well-known good polyfill.

Although, the other way around is proposed by the developers of JSBI library.

This library implements big numbers using its own methods. We can use them instead of native bigints:

Operationnative BigIntJSBI
Creation from Numbera = BigInt(789)a = JSBI.BigInt(789)
Additionc = a + bc = JSBI.add(a, b)
Subtractionc = a - bc = JSBI.subtract(a, b)

…And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them.

In other words, this approach suggests that we write code in JSBI instead of native bigints. But JSBI works with numbers as with bigints internally, emulates them closely following the specification, so the code will be “bigint-ready”.

We can use such JSBI code “as is” for engines that don’t support bigints and for those that do support – the polyfill will convert the calls to native bigints.

References

Common mistakes

  • Skipping the small examples and then missing the exact rule that BigInt depends on.
  • Copying code without changing one value at a time to see which part controls the result.
  • Treating similar-looking syntax or APIs as interchangeable before checking their edge cases.

Summary

  • BigInt gives you one more piece of the JavaScript mental model.
  • The examples in this lesson are the quickest way to check whether the rule is clear.
  • Revisit the key terms when you meet the same idea in later chapters.

Predict

Before running this BigInt check, predict the seven output lines. It covers exact large integers, integer division, mixing errors, loose versus strict equality, and boolean behavior.

javascript
Reveal explanation

The output is 9007199254740993, 2, TypeError, true, false, false, and 1. BigInt keeps integer precision beyond the safe integer range of regular numbers. BigInt division drops the fractional part, so 5n / 2n is 2n. Arithmetic cannot mix BigInt and Number without explicit conversion, hence TypeError. Loose equality may compare numeric value across the two types, while strict equality also checks type. Like 0, 0n is falsy; non-zero BigInts are truthy.

Try it

Convert 2 with BigInt(2) in the mixed addition, then predict which line changes and why the error disappears.

Practice

  • Rewrite one example from this lesson without looking at the original, then run it and compare the result.
  • Change one input, operator, method call, or option in a code sample and predict what will happen before running it.
  • Explain BigInt in your own words as if you were reviewing it with another learner.

Keep learning

Continue with Unicode, String internals when you are ready for the next lesson.