Interview Q&A · JavaScript · 8 questions in this topic
Numbers, Strings & Type Edge Cases — JavaScript Interview Q&A
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
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
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
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.
How do template literals work (interpolation, nesting, tags)?
Answer
Backtick strings embed ${expressions}, span lines, and nest freely — replacing most concatenation. Tagged templates (tag`...`) receive raw strings plus values for DSLs (SQL builders, i18n, styled-components).
Interpolation plus multiline covers daily use; tags are the library-author power tool.
Example
const n = 'Ana', k = 3; console.log(`Hello ${n}, you have ${k * 2} messages`); // tag`SELECT * FROM t WHERE id = ${id}` // safe-query DSL pattern
What is the difference between `slice`, `substring` and `substr`?
Answer
slice(start, end) handles negatives (from end) — the modern choice. substring swaps reversed args and clamps negatives to 0 (legacy quirks). substr(start, length) is deprecated — length-based, do not use.
Default slice; know substring's quirks for legacy code; never write new substr.
Example
'hello'.slice(1, 4); // 'ell' 'hello'.slice(-2); // 'lo' (negatives work) 'hello'.substring(4, 1); // 'ell' (args swapped!)
💡 Slice-default plus substring-swaps is the guidance pair.
What is a `symbol` and what is it good for?
Answer
Symbols are unique, non-string property keys — collision-proof extension points (library metadata, protocols like Symbol.iterator). Well-known symbols customize language behavior (iteration, toPrimitive, instanceof). They are non-enumerable by default (Object.keys skips them).
Unique-keys plus language-protocol hooks is the symbol value proposition.
Example
const id = Symbol('id'); const obj = { [id]: 42 }; // collision-proof key // for (const k of Object.keys(obj)) {} // skips symbols
How do string/number/boolean conversions actually work (valueOf, toString)?
Answer
Objects convert via ToPrimitive: Symbol.toPrimitive first, then valueOf, then toString — the hint (number/string/default) orders attempts. Arrays join (valueOf returns object, toString joins); dates stringify. Explicit String()/Number() trigger the same pipeline deliberately.
[valueOf-then-toString] with hints is the conversion order to recite.
Example
console.log([1, 2] + [3]); // '1,23' (join then concat!) console.log(Number(' 42 ')); // 42 (whitespace trimmed)
What is the difference between `undefined`, undeclared and ReferenceError?
Answer
undefined: declared, no value (safe to read). Undeclared: never declared — reading throws ReferenceError (except typeof guard, which safely yields 'undefined'). Assignment to undeclared names creates globals in sloppy mode, throws in strict mode.
Strict mode ('use strict' / modules) converts the silent-global bug into an error.
Example
// console.log(missing); // ReferenceError console.log(typeof missing); // 'undefined' (safe guard) // undeclared = 1; // sloppy: global leak! strict: throws
💡 typeof-guard plus strict-mode is the safety pairing.
Recommended resourcesAffiliate links · may earn us a commission