The Math Object
Math is a built-in namespace that holds the standard mathematical functions and a few constants. It is not a constructor — there is no new Math(). Think of it as the std::math of JavaScript: Math.abs, Math.sqrt, Math.random, all attached to one object.
The constants
Math.PI; // 3.141592653589793 Math.E; // 2.718281828459045 Math.LN2; // 0.6931... — natural log of 2 Math.LN10; // 2.3025... Math.LOG2E; // log base 2 of e Math.LOG10E; // log base 10 of e Math.SQRT2; // 1.4142... — square root of 2 Math.SQRT1_2; // ~0.7071 — square root of 1/2
Absolute value, sign, rounding
Math.abs(-5); // 5 Math.abs(5); // 5 Math.sign(-7); // -1 Math.sign(0); // 0 Math.sign(7); // 1 Math.sign(NaN); // NaN // Four ways to drop the decimals — read carefully Math.floor(2.9); // 2 — towards -Infinity Math.floor(-2.1); // -3 Math.ceil(2.1); // 3 — towards +Infinity Math.ceil(-2.9); // -2 Math.round(2.5); // 3 — half-up (but -0.5 rounds to 0) Math.round(-2.5); // -2 — note the asymmetry! Math.trunc(2.9); // 2 — drop the decimals, no rounding Math.trunc(-2.9); // -2
min, max, clamp
Math.max(3, 1, 4, 1, 5, 9); // 9 Math.min(3, 1, 4, 1, 5, 9); // 1 // Spread an array const xs = [3, 1, 4, 1, 5, 9]; Math.max(...xs); // 9 // Identity values when no args are passed Math.max(); // -Infinity Math.min(); // Infinity // One-liner clamp — restrict v between lo and hi const clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi); clamp(15, 0, 10); // 10 clamp(-1, 0, 10); // 0 clamp(7, 0, 10); // 7
Powers, roots, logs and exponentials
Math.pow(2, 10); // 1024 — same as 2 ** 10 Math.sqrt(81); // 9 Math.cbrt(27); // 3 — cube root Math.hypot(3, 4); // 5 — sqrt(a^2 + b^2 + ...), no overflow Math.log(Math.E); // 1 — natural log (base e) Math.log2(1024); // 10 Math.log10(1000); // 3 Math.exp(1); // 2.718... — e^x Math.expm1(0.0001); // 0.0001000050... — accurate e^x - 1 near zero
Trigonometry
All trig functions take radians, not degrees.
const toRadians = deg => deg * Math.PI / 180; const toDegrees = rad => rad * 180 / Math.PI; Math.sin(0); // 0 Math.cos(Math.PI); // -1 (close to it) Math.tan(Math.PI / 4); // 1 (close to it) Math.atan2(1, 1); // π/4 — full-circle angle for (x, y) toDegrees(Math.atan2(1, 1)); // 45 // Inverse functions Math.asin(1); // π/2 Math.acos(0); // π/2 Math.atan(1); // π/4
Random numbers — the recipes you keep googling
Math.random() returns a pseudo-random number in the half-open range [0, 1). Everything else is recipes built on top of it.
// Random float in [min, max) const randFloat = (min, max) => Math.random() * (max - min) + min; // Random integer in [min, max] inclusive const randInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; // Pick a random element from an array const pick = arr => arr[Math.floor(Math.random() * arr.length)]; // Coin flip const flip = () => Math.random() < 0.5 ? "heads" : "tails"; console.log(randInt(1, 6)); // a fair dice roll
One-liners worth memorising
// Distance between two 2-D points const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y); // Sum of an array (built-in alternative below) const sum = xs => xs.reduce((a, b) => a + b, 0); // Average const mean = xs => sum(xs) / xs.length; // Map a value from one range to another (linear interpolation) const lerp = (a, b, t) => a + (b - a) * t; const map = (v, inMin, inMax, outMin, outMax) => outMin + ((v - inMin) / (inMax - inMin)) * (outMax - outMin); // Round to N decimal places const roundTo = (v, n) => Math.round(v * 10 ** n) / 10 ** n; // Degrees <-> radians const deg = r => r * 180 / Math.PI; const rad = d => d * Math.PI / 180;
Less-known but useful
Math.fround(x)— round to nearest 32-bit float. Useful when interfacing withFloat32Arrayor WebGL.Math.clz32(x)— count of leading zero bits in the 32-bit binary form ofx. Bit-manipulation territory.Math.imul(a, b)— true 32-bit integer multiplication. JavaScript multiplication of large integers loses precision;imulis faithful in 32-bit space.Math.sinh,Math.cosh,Math.tanhand their inverses — hyperbolic functions.
Quick demo
console.log(Math.hypot(6, 8)); // 10 console.log(Math.clz32(1)); // 31 — only the lowest bit is set console.log(Math.fround(1.1)); // 1.100000023841858 — rounded to f32
10 31 1.100000023841858
Most day-to-day code uses no more than a dozen Math methods. Keep them in your head — abs, min, max, floor, round, pow/**, sqrt, random — and reach for the documentation only when you need the more specialised tools.