JavaScript Notes
Master JavaScript Numbers: IEEE 754 floating point, precision issues, NaN, Infinity, BigInt, Number methods like toFixed, parseInt, parseFloat, and numeric interview questions with examples.
JavaScript has one number type for all numeric values — both integers and decimals. It uses the IEEE 754 double-precision floating-point standard (64-bit), the same format used by most programming languages. Understanding this standard explains JavaScript's famous floating-point quirks.
💡 Key insight: JavaScript doesn't have separateintandfloattypes.42and42.0are the exact same value. This simplicity introduces subtle precision issues with decimals that every JavaScript developer must know.
IEEE 754 — Why 0.1 + 0.2 !== 0.3
console.log(0.1 + 0.2); // Not 0.3!
console.log(0.1 + 0.2 === 0.3); // false!
console.log(0.1 + 0.2 - 0.3 < Number.EPSILON); // true — safe comparison0.30000000000000004 false true
Why? 0.1 and 0.2 cannot be represented exactly in binary (like 1/3 in decimal). The tiny rounding errors accumulate.
Safe float comparison:
function areEqual(a, b, epsilon = Number.EPSILON) {
return Math.abs(a - b) < epsilon;
}
console.log(areEqual(0.1 + 0.2, 0.3)); // true
console.log(Number.EPSILON); // 2.220446049250313e-16true 2.220446049250313e-16
Special Numeric Values
console.log(Infinity); // positive infinity
console.log(-Infinity); // negative infinity
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity
console.log(Infinity + 1); // Infinity
console.log(isFinite(Infinity)); // false
console.log(NaN); // Not a Number
console.log(typeof NaN); // "number" (surprising!)
console.log(NaN === NaN); // false (NaN is not equal to itself)
console.log(isNaN("hello")); // true (coerces first)
console.log(Number.isNaN("hello")); // false (strict check)
console.log(Number.isNaN(NaN)); // trueInfinity -Infinity Infinity -Infinity Infinity false NaN "number" false true false true
⚠️typeof NaN === "number"— this is a historical quirk.NaNmeans "invalid number result", not "not a number type." Always useNumber.isNaN()(strict), never globalisNaN()(which coerces the argument first).
Safe Integer Range
console.log(Number.MAX_SAFE_INTEGER); // 2^53 - 1
console.log(Number.MIN_SAFE_INTEGER); // -(2^53 - 1)
console.log(Number.MAX_VALUE); // largest finite number
console.log(Number.MIN_VALUE); // smallest positive number > 0
// Beyond safe integer range — precision is lost!
console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992 — SAME!
console.log(Number.isSafeInteger(9007199254740991)); // true
console.log(Number.isSafeInteger(9007199254740992)); // false9007199254740991 -9007199254740991 1.7976931348623157e+308 5e-324 9007199254740992 9007199254740992 true false
BigInt — For Very Large Integers
// Regular number loses precision
console.log(9007199254740993); // 9007199254740992 (wrong!)
// BigInt preserves precision
console.log(9007199254740993n); // 9007199254740993n (correct!)
console.log(typeof 9007199254740993n); // "bigint"
const big = BigInt("99999999999999999999999999999");
console.log(big + 1n); // 100000000000000000000000000000n
console.log(Number(big)); // loses precision when converted9007199254740992 9007199254740993n "bigint" 100000000000000000000000000000n 1e+29
You cannot mixBigIntand regular numbers in operations:1n + 1throws aTypeError. Convert explicitly:Number(bigInt)orBigInt(number).
Number Methods
const n = 3.14159;
// Round to decimal places
console.log(n.toFixed(2)); // "3.14" (string!)
console.log(n.toFixed(4)); // "3.1416"
// Significant digits
console.log(n.toPrecision(4)); // "3.142"
console.log(n.toPrecision(2)); // "3.1"
// Convert to string in different bases
console.log((255).toString(16)); // "ff" (hex)
console.log((255).toString(2)); // "11111111" (binary)
console.log((255).toString(8)); // "377" (octal)
// Exponential notation
console.log((1234567).toExponential(2)); // "1.23e+6""3.14" "3.1416" "3.142" "3.1" "ff" "11111111" "377" "1.23e+6"
Parsing Numbers
// Convert string to number
console.log(Number("42")); // 42
console.log(Number("3.14")); // 3.14
console.log(Number("")); // 0
console.log(Number("abc")); // NaN
console.log(Number(true)); // 1
console.log(Number(false)); // 0
console.log(Number(null)); // 0
console.log(Number(undefined)); // NaN
// parseInt — reads integer, stops at non-numeric character
console.log(parseInt("42px")); // 42
console.log(parseInt("0xFF", 16)); // 255 (hex)
console.log(parseInt("3.9")); // 3 (truncates decimal)
// parseFloat — reads float, stops at non-numeric character
console.log(parseFloat("3.14em")); // 3.14
console.log(parseFloat("abc")); // NaN
// Unary + (fastest coercion)
console.log(+"42"); // 42
console.log(+""); // 0
console.log(+"abc"); // NaN42 3.14 0 NaN 1 0 0 NaN 42 255 3 3.14 NaN 42 0 NaN
Math Object Quick Reference
console.log(Math.floor(4.7)); // 4 — round down
console.log(Math.ceil(4.1)); // 5 — round up
console.log(Math.round(4.5)); // 5 — round to nearest
console.log(Math.trunc(4.9)); // 4 — remove decimal (no rounding)
console.log(Math.abs(-7)); // 7 — absolute value
console.log(Math.pow(2, 10)); // 1024
console.log(Math.sqrt(144)); // 12
console.log(Math.max(1,5,3,9)); // 9
console.log(Math.min(1,5,3,9)); // 1
console.log(Math.PI); // 3.141592653589793
// Random integer between min and max (inclusive)
function randInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(randInt(1, 6)); // simulates a dice roll4 5 5 4 7 1024 12 9 1 3.141592653589793 4 // (random — yours will differ)
Numeric Literals
console.log(0xFF); // 255 — hex
console.log(0b11111111); // 255 — binary
console.log(0o377); // 255 — octal
console.log(1_000_000); // 1000000 — numeric separator (ES2021)
console.log(1.5e3); // 1500 — scientific notation
console.log(1.5e-3); // 0.0015255 255 255 1000000 1500 0.0015
Common Mistakes
0.1 + 0.2 !== 0.3— never compare floats with===. UseMath.abs(a - b) < Number.EPSILON.NaN === NaNisfalse— useNumber.isNaN()to check for NaN.isNaN("hello")returnstrue— globalisNaNcoerces the argument first. UseNumber.isNaN()for strict checks.- Integer overflow beyond
MAX_SAFE_INTEGER— useBigIntfor IDs or values above 2^53. toFixedreturns a string —(3.14).toFixed(1)returns"3.1"not3.1. Wrap inNumber()or+if you need a number.
Interview Questions
Q1. Why does 0.1 + 0.2 not equal 0.3 in JavaScript?
JavaScript numbers use IEEE 754 double-precision floating point.0.1and0.2cannot be represented exactly in binary — their binary representations are infinite repeating fractions that get rounded. When added, the rounding errors accumulate, producing0.30000000000000004.
Q2. What is NaN and why is typeof NaN === "number"?
NaN(Not a Number) is a special numeric value returned when a math operation fails (e.g.,"abc" * 2).typeof NaN === "number"is a historical quirk —NaNis technically of number type but represents an invalid numeric result.
Q3. What is the difference between isNaN() and Number.isNaN()?
GlobalisNaN(x)coercesxto a number first, soisNaN("abc")returnstrue(becauseNumber("abc")is NaN).Number.isNaN(x)does not coerce — it returnstrueonly ifxis already the valueNaN. PreferNumber.isNaN().
Q4. What is Number.MAX_SAFE_INTEGER and why does it matter?
It's2^53 - 1 = 9,007,199,254,740,991. Above this, JavaScript integers lose precision because the floating-point representation can't distinguish consecutive integers. UseBigIntfor values beyond this limit.
Q5. How do you safely round a number to 2 decimal places?
UseMath.round(num * 100) / 100(returns a number) ornum.toFixed(2)(returns a string, then convert withNumber()). Avoid direct rounding of float operations due to precision issues.
Q6. What does parseInt("09") return?
9. In modern JavaScript,parseIntdefaults to base 10. However, always explicitly pass the radix:parseInt("09", 10)— especially when processing user input.
Q7. What is Number.EPSILON used for?
It's the smallest difference between two distinct numbers that JavaScript can represent (~2.22e-16). Used for safe float comparison: Math.abs(a - b) < Number.EPSILON to check if two floats are "equal enough."Key Takeaways
- JavaScript has one number type (IEEE 754 double-precision) — no separate int/float.
0.1 + 0.2 !== 0.3— compare floats withMath.abs(a - b) < Number.EPSILON.NaN !== NaN— always useNumber.isNaN()to detect NaN.- Safe integer range is ±(2^53 - 1) — use
BigIntfor larger values. parseInt/parseFloatparse strings; always pass radix toparseInt.toFixed()returns a string — convert back to number if needed.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Numbers — IEEE 754, Precision, and Number Methods Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, data, structures, numbers
Related JavaScript Master Course Topics