Interview Q&A · JavaScript · 8 questions in this topic

Numbers, Strings & Type Edge Cases — JavaScript Interview Q&A

All JavaScript topics

The numeric and string gotchas — NaN, floating point, BigInt, symbols and template literals. Part of JavaScript Fundamentals · 204 questions in the JavaScript bank.

Why is `0.1 + 0.2 !== 0.3`? How do you handle money?

Answer

IEEE-754 binary floating point cannot represent most decimals exactly — 0.1 + 0.2 yields 0.30000000000000004. Money needs integers (cents) or decimal libraries; comparisons need epsilon tolerance (Number.EPSILON).

Floats are for measurement; integers/decimals are for money — the domain rule.

Example

Example.jsjs
console.log(0.1 + 0.2); // 0.30000000000000004
const cents = 10 + 20; // 30 cents: integers stay exact
// Math.abs(a - b) < Number.EPSILON // tolerant compare

💡 Cents-not-floats is the money answer; EPSILON is the comparison answer.

What is `NaN` and how do you reliably detect it?

Answer

NaN (Not-a-Number) is the failed-number-computation value — the only value unequal to itself. Global isNaN coerces first (isNaN('hi') is true — useless); Number.isNaN checks without coercion; Number.isFinite validates real numbers.

Always Number.isNaN — the global version's coercion makes it a lie detector that lies.

Example

Example.jsjs
console.log(Number.isNaN('hi')); // false (correct: not NaN)
console.log(isNaN('hi'));          // true (coerced — misleading)
console.log(Number.isFinite(1/0)); // false

What are the limits of `number` (MAX_SAFE_INTEGER) and what is BigInt for?

Answer

Doubles represent integers exactly only to 2^53 - 1 (Number.MAX_SAFE_INTEGER); beyond that, arithmetic silently rounds — IDs and counters corrupt. BigInt (123n) gives arbitrary-precision integers with its own operators (no mixing with number without explicit conversion).

IDs/counters past 2^53 want BigInt or strings; JSON needs custom handling (no native BigInt support).

Example

Example.jsjs
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
const big = 9007199254740993n;
// big + 1; // TypeError if mixed with number: convert first

💡 2^53 boundary plus no-mixing rule is the complete BigInt answer.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits