Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Types  ›  Lesson

BigInt and Symbol

bigint 12 minBigInt stores whole numbers of any size exactly but cannot mix with ordinary numbers, and Symbol creates unique values used as property keys that cannot clash.

BigInt and Symbol

In short: two primitive types exist for problems the other five can't solve. Number.MAX_SAFE_INTEGER (9007199254740991) is the largest whole number an ordinary number keeps exact — past it, results quietly round. BigInt (a trailing n) stores any whole number exactly, but never mixes with a plain number in arithmetic. Symbol creates a value that is guaranteed unique, even against another symbol with the identical description — useful for object keys that must never clash.

Left: an ordinary number shelf where the value past the safe limit silently rounds and falls off the edge, next to an exact BigInt track with a trailing n; right: two separate Symbol("id") calls stamping two different, unequal values

A stream counter that stops climbing

A hit song's total stream count can pass nine quadrillion. An ordinary number can't hold every value that large exactly:

javascript

Adding 1 does nothing — the true result needs more precision than a 64-bit float has, so it rounds right back to where it started. The counter looks frozen, and nothing in the code raised an error to tell you.

Switching that counter to BigInt

Add a trailing n and the same counter becomes exact at any size:

javascript

9007199254740992n + 1n gives the correct 9007199254740993n. Division rounds toward zero because BigInt only holds whole numbers — 7n / 2n is 3n, never 3.5n.

The catch: BigInt and ordinary number can never appear on the same side of an arithmetic operator.

javascript

Mixing them throws a TypeError. To combine with a normal number, convert it on purpose: bigStreams + BigInt(1).

Counting streams past the safe limitStep 1 of 4
  1. Try an ordinary number

    9007199254740992 + 1 should be ...993, but it gives 9007199254740992: past the safe limit, the result rounds.

Tagging an object without a name clash

A logging library wants to attach a hidden id to any object you pass it, without ever overwriting a property you already named id:

javascript

Every Symbol() call makes a brand-new value — the text "id" is only a debugging label, not part of its identity, so id1 === id2 is false. A symbol key can only be reached by someone holding that exact symbol, and it is skipped by JSON.stringify and for...in, so it never collides with an ordinary property name.

Quick practice

quick drill

What is typeof 10n?

quick drill

What happens when 5n + 2 runs?

quick drill

What is 9n / 2n?

quick drill

What does Symbol("key") === Symbol("key") evaluate to?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhen numbers stop being exact

A stream counter stored as a plain number is at 9007199254740992 and adding 1 leaves it unchanged. Explain why, how BigInt fixes it, and what mistake would cause a TypeError after switching.