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 trailingn(123n) or viaBigInt(123), with no upper bound and no precision loss, unlike regularnumbers. - 📌 Interview term: the real, direct answer to the prompt — verified directly: a value beyond
Number.MAX_SAFE_INTEGER(9007199254740991) written as a regularNumbergenuinely loses precision (rounds to the nearest representable double), while the identical value written as aBigIntliteral genuinely preserves it exactly. - 📌 Interview term: BigInt and Number cannot mix implicitly — verified directly: adding a
BigIntand aNumberdirectly (1n + 1) genuinely throws a realTypeError— an explicit conversion (1n + BigInt(1), orNumber(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 / 2ngenuinely produces3n, not3.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 acrossBigInt/Number(1n == 1istrue, verified directly), while strict equality (===) genuinely does not (1n === 1isfalse, 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.