Skip to the solution
MediumFrontend6 min read

What is BigInt?

214 views

The problem

Question presented to candidate: "If you need to work with an integer larger than Number.MAX_SAFE_INTEGER, what actually goes wrong if you just use a regular number, and how does BigInt fix it?"

What a strong answer should cover:

  • πŸ“Œ Interview term: BigInt β€” a distinct primitive type representing arbitrarily large integers exactly, written with a trailing n (123n) or via BigInt(123), with no upper bound and no precision loss, unlike regular numbers.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: a value beyond Number.MAX_SAFE_INTEGER (9007199254740991) written as a regular Number genuinely loses precision (rounds to the nearest representable double), while the identical value written as a BigInt literal genuinely preserves it exactly.
  • πŸ“Œ Interview term: BigInt and Number cannot mix implicitly β€” verified directly: adding a BigInt and a Number directly (1n + 1) genuinely throws a real TypeError β€” an explicit conversion (1n + BigInt(1), or Number(1n) + 1) is genuinely required, a deliberate design choice preventing silent precision loss from sneaking into BigInt arithmetic.
  • πŸ“Œ Interview term: BigInt division truncates β€” verified directly: 7n / 2n genuinely produces 3n, not 3.5n β€” BigInt division always rounds toward zero, since a BigInt can never represent a fraction at all.
  • A precise answer names that loose equality (==) genuinely works across BigInt/Number (1n == 1 is true, verified directly), while strict equality (===) genuinely does not (1n === 1 is false, since they are different types) β€” the same type-vs-value distinction covered in this bank's own == vs. === question.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering what specifically breaks with a regular Number (silent precision loss, not a crash) is the strong signal.

Code / implementation expected: Yes β€” the direct side-by-side precision-loss comparison between a large Number and the equivalent BigInt is the clearest, most convincing demonstration.

  • bigint

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a Number beyond MAX_SAFE_INTEGER silently loses precision while the equivalent BigInt preserves it exactly

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 109 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How does async/await work?

Continue