JavaScript has one number type — IEEE 754 double-precision 64-bit floating point. That single fact explains NaN, Infinity, 0.1 + 0.2 !== 0.3, and +0/-0. This article covers Number methods, the Math object, and the traps interviewers test.
1. Number Representation
JavaScript uses 64-bit IEEE 754. Integers are safe up to 2^53 - 1; beyond that precision degrades.
Loading editor...
For integers larger than MAX_SAFE_INTEGER, use BigInt:
Loading editor...
2. NaN — Not a Number
NaN is the result of a failed numeric operation. It's the only value not equal to itself.
Loading editor...
Interview use: Deep equality checks (#14), JSON.stringify (NaN → null).
3. Infinity and Finite Checks
Loading editor...
Interview use: JSON.stringify (Infinity → null), range checks (#30).
4. +0 and -0
IEEE 754 has signed zero. +0 === -0 is true, but Object.is(+0, -0) is false.
Loading editor...
Interview use: Deep equality — Object.is or custom check (#14).
5. Floating-Point Precision
0.1 + 0.2 is not 0.3 — it's a binary representation artifact:
Loading editor...
6. Number Static Methods
Number.isInteger(value) — Integer Check
Loading editor...
Number.parseFloat(str) / Number.parseInt(str, radix)
Loading editor...
Number.prototype.toFixed(digits) — Format
Loading editor...
7. The Math Object
Rounding
Loading editor...
Min, Max, Range
Loading editor...
Interview use: Range filters in data selection (#30).
Random
Loading editor...
Other Useful Math Methods
Loading editor...
Quick Reference
| Category | Methods |
|---|---|
| Check | Number.isNaN(v), Number.isFinite(v), Number.isInteger(v), Number.isSafeInteger(v) |
| Parse | Number.parseInt(s, r), Number.parseFloat(s) |
| Round | Math.floor, Math.ceil, Math.round, Math.trunc |
| Min/Max | Math.min(...a), Math.max(...a) |
| Random | Math.random() |
| Other | Math.abs, Math.sqrt, Math.pow, Math.sign |
| Equality | Object.is(a, b) — handles NaN and +0/-0 correctly |
Interview Tips
- Use
Number.isNaN, notisNaN— the global version coerces strings to numbers. - Use
Number.isFinite, notisFinite— same reason. - Use
Object.isfor strict comparisons — handles NaN and signed zero. - Clamp with
Math.min(Math.max(v, min), max)— the one-liner for range constraints.