JavaScript Math Reference
Every Math method and constant — with real examples, edge cases, and the
Math.round(-0.5) gotcha that surprises everyone.
Rounding
Math.round
Returns the value of a number rounded to the nearest integer. Halfway values (.5) always round toward +Infinity, not away from zero.
Math.floor
Returns the largest integer less than or equal to a given number (rounds toward -Infinity). Always moves left on the number line.
Math.ceil
Returns the smallest integer greater than or equal to a given number (rounds toward +Infinity). Always moves right on the number line.
Math.trunc
Returns the integer part of a number by removing any fractional digits — rounding toward zero. Introduced in ES2015.
Math.sign
Returns the sign of a number as 1, -1, 0, -0, or NaN. Introduced in ES2015 as a compact replacement for the ternary `x > 0 ? 1 : x < 0 ? -1 : 0`.
Basic Arithmetic
Math.abs
Returns the absolute value of a number — its distance from zero without regard to sign. Useful for differences, magnitudes, and tolerance checks.
Math.pow
Returns base raised to the power of exponent. In modern code the `**` operator (ES2016) is usually preferred for readability and identical semantics.
Math.sqrt
Returns the positive square root of a number. Equivalent to Math.pow(x, 0.5) but faster and clearer.
Math.cbrt
Returns the cube root of a number. Introduced in ES2015, and unlike sqrt it handles negative inputs correctly.
Math.hypot
Returns the square root of the sum of squares of its arguments — the Euclidean norm. Introduced in ES2015 and safer than manual sqrt(a*a + b*b) against overflow.
Exponential & Log
Math.exp
Returns e^x, where e is Euler's number (~2.71828). The inverse of Math.log (the natural logarithm).
Math.log
Returns the natural logarithm (base e, or ln) of a number. Inverse of Math.exp.
Math.log2
Returns the base-2 logarithm of a number. Introduced in ES2015 and useful for information theory, tree depths, and bit counts.
Math.log10
Returns the base-10 logarithm of a number. Introduced in ES2015 for accurate common-log calculations (decibels, pH, orders of magnitude).
Math.expm1
Returns e^x - 1, computed accurately even for very small x. Introduced in ES2015 to avoid catastrophic cancellation near zero.
Math.log1p
Returns the natural log of (1 + x), computed accurately even for very small x. Introduced in ES2015 as the companion to expm1.
Trigonometry
Math.sin
Returns the sine of an angle expressed in radians. Result is always in the range [-1, 1].
Math.cos
Returns the cosine of an angle expressed in radians. Result is always in [-1, 1].
Math.tan
Returns the tangent of an angle expressed in radians. Equal to sin(x) / cos(x) and unbounded near π/2 + kπ.
Math.atan2
Returns the angle in radians between the positive x-axis and the point (x, y), in the range (-π, π]. Note the argument order: y first, then x.