SKU // 83

JavaScript Math Reference

This lesson is a complete reference for the Math object, grouped by purpose, followed by the two areas that need extra explanation: trigonometry (which works in radians, not degrees) and logarithms.

// Rounding and Parts

round(x)Nearest integerround(2.5) → 3
floor(x)Round downfloor(-2.5) → -3
ceil(x)Round upceil(-2.5) → -2
trunc(x)Drop decimalstrunc(-2.7) → -2
abs(x)Absolute valueabs(-3) → 3
sign(x)-1, 0, or 1sign(-8) → -1

// Powers, Roots, Logs

pow(x, y)x to the power y (same as x ** y)pow(2, 10) → 1024
sqrt(x) / cbrt(x)Square / cube rootcbrt(27) → 3
hypot(a, b, ...)Square root of the sum of squareshypot(3, 4) → 5
exp(x)e to the power xexp(1) → 2.718...
log(x)Natural log (base e)log(Math.E) → 1
log10(x)Base-10 loglog10(1000) → 3
log2(x)Base-2 loglog2(8) → 3

// Comparing and Random

max(...) / min(...)Largest / smallest argumentmax(1, 9, 4) → 9
random()Decimal from 0 up to (not including) 1see next lesson

// Trigonometry

sin(x) / cos(x) / tan(x)x is an angle in radians
asin(x) / acos(x) / atan(x)Inverse: returns an angle in radians
atan2(y, x)Angle from the origin to the point (x, y)

// Constants

PI3.141592653589793
E2.718281828459045
SQRT2 / SQRT1_2√2 ≈ 1.414, √½ ≈ 0.707
LN2 / LN10 / LOG2E / LOG10Elog constants, e.g. LN2 ≈ 0.693

// Specialist Methods

The full Math object has a few more methods. You will rarely need them in this course, but you should recognize them:

sinh / cosh / tanhHyperbolic sine, cosine, tangentcosh(0) → 1
asinh / acosh / atanhInverse hyperbolic functionsacosh(1) → 0
expm1(x) / log1p(x)ex - 1 and log(1 + x), exact for tiny xexpm1(0) → 0
fround(x)Nearest 32-bit (single precision) floatfround(5.05) → 5.050000190734863
f16round(x)Nearest 16-bit (half precision) floatf16round(5.05) → 5.05078125
clz32(x)Count leading zero bits in 32-bit binaryclz32(1) → 31
imul(a, b)32-bit integer multiplyimul(3, 4) → 12
W3Schools error
W3Schools' reference describes f16round(x) as "rounded downwards to the nearest integer," the same text it uses for floor. That is wrong: it rounds to the nearest 16-bit float, so Math.f16round(2.7) is 2.69921875, not 2. Its list also leaves out hypot and imul, which are real Math methods.
Trap: impossible inputs give NaN
asin and acos only accept -1 to 1, because sine and cosine never go outside that range. Math.acos(2) is NaN, just like Math.sqrt(-1) and Math.log(-1). No error is thrown, so check your inputs.

// Trig Uses Radians, Not Degrees

A full circle is 360 degrees, or 2π radians. JavaScript's trig functions only speak radians, so convert first:

const toRad = deg => deg * Math.PI / 180;
const toDeg = rad => rad * 180 / Math.PI;

Math.sin(30)            // -0.988...  (30 RADIANS: not what you meant)
Math.sin(toRad(30))     // 0.49999999999999994  (about 0.5, correct)
-0.9880316240928618 0.49999999999999994
Watch out
Results are often a hair off the textbook answer, like 0.49999999999999994 instead of 0.5, or Math.sin(Math.PI) giving 1.22e-16 instead of 0. That is the decimal precision issue again. Round the result for display.

// Practical Trig: Distance and Angle

Two things come up constantly in games and graphics: how far apart two points are, and which direction one is from the other.

const player = { x: 2, y: 3 };
const enemy  = { x: 5, y: 7 };

const dx = enemy.x - player.x;   // 3
const dy = enemy.y - player.y;   // 4

const distance = Math.hypot(dx, dy);
const angle = toDeg(Math.atan2(dy, dx));

console.log(distance, angle.toFixed(1));
5 53.1
Tip
Use atan2(y, x), not atan(y / x). atan2 knows which quadrant the point is in and handles x = 0 without dividing by zero. Note the order: y first.

// Logarithms in Plain Terms

A log answers "what power do I need?" Math.log10(1000) is 3 because 103 = 1000. A practical use: counting digits.

const digits = n => Math.floor(Math.log10(n)) + 1;

digits(7)        // 1
digits(4096)     // 4
digits(1000000)  // 7
1 4 7

// Quick Check

What does this log?

console.log(Math.hypot(6, 8), Math.log2(32));
Show answer
10 5. The square root of 36 + 64 is 10, and 2 multiplied 5 times is 32.

A player is at (0, 0) and a target is straight above at (0, 10). Which call gives the angle safely?

  1. Math.atan(10 / 0)
  2. Math.atan2(10, 0)
  3. Math.atan2(0, 10)
  4. Math.tan(10, 0)
Show answer
B. atan2 takes y first and handles x = 0 without dividing by zero. C has the arguments backwards (it gives 0, pointing right).

Math.sin(Math.PI) returns 1.2246467991473532e-16. What does that mean?

  1. Your browser has a bug
  2. It is a tiny number, essentially 0, caused by decimal precision
  3. The angle should have been in degrees
  4. It is NaN
Show answer
B. Math.PI is not exactly pi, so the answer is a hair off 0 (0.00000000000000012). Round results for display.

// Try It

  1. Click Open Playground. Compute Math.cos of 0, 90, and 180 degrees (convert first) and round the results
  2. Find the distance between (1, 1) and (7, 9) with Math.hypot
  3. How many times do you double 1 to pass one million? Use Math.log2 and Math.ceil
  4. Use digits() on several numbers, then work out why it fails for 0
// Video Walkthroughs
Last updated: Oct 10, 2026, 1:46 AM ET