Numbers are one of the most fundamental parts of any JavaScript program — yet most developers only scratch the surface of what is available. This guide walks through every important number method and Math object utility, with practical examples you can use right away.
Numbers in JavaScript
JavaScript has only one number type — it uses 64-bit floating point (IEEE 754) for all numbers, both integers and decimals. This means there is no separate int or float type like in other languages. It also explains some of the quirky behaviour you may have encountered.
typeof 42 → "number" typeof 3.14 → "number" typeof NaN → "number" ← NaN is technically a number type typeof Infinity → "number"
Special number values
JavaScript has three special numeric values worth knowing before you dive into methods.
NaN — Not a Number
NaN is the result of an invalid numeric operation. The confusing part: NaN is not equal to anything, including itself.
"hello" * 2 → NaN undefined + 1 → NaN 0 / 0 → NaN NaN === NaN → false ← NaN never equals itself! isNaN("hello") → true Number.isNaN(NaN) → true ← safer, more precise check
Prefer Number.isNaN() over isNaN(). The global isNaN() coerces values first — isNaN("hello") returns true even though "hello" is a string, not NaN. Number.isNaN() only returns true for actual NaN values.
Infinity and -Infinity
1 / 0 → Infinity -1 / 0 → -Infinity Infinity + 1 → Infinity isFinite(100) → true isFinite(Infinity) → false
Number methods
toFixed()
Formats a number to a fixed number of decimal places and returns it as a string. Most useful for displaying prices or measurements.
(3.14159).toFixed(2) → "3.14" (3.14159).toFixed(4) → "3.1416" ← rounds up (5).toFixed(2) → "5.00" (1.005).toFixed(2) → "1.00" ← floating point quirk!
toPrecision()
Formats a number to a specified total number of significant digits.
(123.456).toPrecision(5) → "123.46" (0.00123).toPrecision(2) → "0.0012" (123456).toPrecision(4) → "1.235e+5"
toString()
Converts a number to a string. Can also convert to a different base (binary, hex, octal).
(255).toString() → "255" (255).toString(2) → "11111111" ← binary (255).toString(16) → "ff" ← hexadecimal (255).toString(8) → "377" ← octal
Number.isInteger()
Returns true if the value is an integer — no decimal part.
Number.isInteger(42) → true Number.isInteger(42.0) → true ← 42.0 is still an integer Number.isInteger(42.5) → false Number.isInteger("42") → false ← string, not number
Number.isFinite()
Returns true if the value is a finite number — not Infinity, -Infinity, or NaN.
Number.isFinite(100) → true Number.isFinite(Infinity) → false Number.isFinite(NaN) → false Number.isFinite("100") → false ← does not coerce
parseInt() and parseFloat()
Parse a string and extract the leading number from it. Very useful when dealing with user input or CSS values.
parseInt("42px") → 42 parseInt("3.9") → 3 ← truncates decimal parseInt("0xff", 16) → 255 ← parse hex parseFloat("3.14em") → 3.14 parseFloat("abc") → NaN ← no leading number
The Math object
The Math object is a built-in JavaScript object that provides mathematical constants and functions. You do not create it — just use it directly.
Math.round(), Math.floor(), Math.ceil()
Math.round(4.5) → 5 ← rounds to nearest integer Math.round(4.4) → 4 Math.floor(4.9) → 4 ← always rounds DOWN Math.ceil(4.1) → 5 ← always rounds UP Math.trunc(4.9) → 4 ← removes decimal, no rounding Math.trunc(-4.9) → -4 ← Math.floor(-4.9) = -5 (different!)
Math.abs()
Returns the absolute (positive) value of a number.
Math.abs(-5) → 5 Math.abs(5) → 5 Math.abs(-3.14) → 3.14
Math.max() and Math.min()
Returns the largest or smallest of the given values.
Math.max(1, 5, 3, 9, 2) → 9 Math.min(1, 5, 3, 9, 2) → 1 // With an array — use spread operator let scores = [88, 95, 72, 100, 63]; Math.max(...scores) → 100 Math.min(...scores) → 63
Math.pow() and Math.sqrt()
Math.pow(2, 10) → 1024 ← 2 to the power of 10 Math.pow(3, 3) → 27 Math.sqrt(144) → 12 Math.sqrt(2) → 1.4142135623730951
Math.random()
Returns a random floating-point number between 0 (inclusive) and 1 (exclusive). Almost always combined with Math.floor() to get useful random integers.
Math.random() → 0.7342... (varies) // Random integer between 0 and 9 Math.floor(Math.random() * 10) → 0 to 9 // Random integer between 1 and 10 Math.floor(Math.random() * 10) + 1 → 1 to 10 // Random integer between min and max (inclusive) function randInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } randInt(5, 15) → any integer from 5 to 15
Math constants
Math.PI → 3.141592653589793 Math.E → 2.718281828459045 ← Euler's number Math.SQRT2 → 1.4142135623730951 Math.LN2 → 0.6931471805599453
The floating point problem — why 0.1 + 0.2 !== 0.3
This is one of the most famous JavaScript gotchas. Because JavaScript uses binary floating-point arithmetic, some decimal values cannot be represented exactly.
0.1 + 0.2 → 0.30000000000000004 0.1 + 0.2 === 0.3 → false ← !! // Fix 1: toFixed() for display (0.1 + 0.2).toFixed(2) → "0.30" // Fix 2: multiply to avoid decimals (0.1 * 10 + 0.2 * 10) / 10 → 0.3 // Fix 3: use Number.EPSILON for comparison Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON → true
Never compare floating-point numbers with === directly. Use toFixed() for display, integer multiplication for calculation, or Number.EPSILON for comparison.
Quick reference — all methods at a glance
── Number methods ──────────────────────────── toFixed(n) → fixed decimal places (returns string) toPrecision(n) → total significant digits toString(base) → convert to string (optional base) Number.isNaN() → safe NaN check Number.isFinite() → safe finite check Number.isInteger() → check if integer parseInt() → parse string to integer parseFloat() → parse string to float ── Math object ─────────────────────────────── Math.round() → round to nearest integer Math.floor() → always round down Math.ceil() → always round up Math.trunc() → remove decimal part Math.abs() → absolute value Math.max() → largest of values Math.min() → smallest of values Math.pow(x, y) → x to the power of y Math.sqrt() → square root Math.random() → random float 0–1 Math.PI → 3.14159...
Key takeaways
JavaScript has only one number type — 64-bit floating point — which handles both integers and decimals. Special values NaN, Infinity, and -Infinity each have their own behaviours. Use Number.isNaN() and Number.isFinite() instead of their global counterparts for safer checks. The Math object gives you everything from rounding and random numbers to powers and square roots without any imports. Never compare floating-point numbers directly with === — use toFixed() for display or Number.EPSILON for precise comparisons.