TExcelTAD
Home › JavaScript › JS Math

JavaScript Math

Math is a built-in object full of mathematical constants and functions: rounding, powers, roots, trigonometry, minimum and maximum, and random numbers. You don't create it with new — just call its methods directly, like Math.round(4.7).

Constants

js · try it yourself
console.log(Math.PI);
console.log(Math.E);
console.log(Math.SQRT2);
console.log(Math.LN10);
3.141592653589793
2.718281828459045
1.4142135623730951
2.302585092994046

Rounding

There are four ways to turn a decimal into a whole number, and they differ for negative numbers and halves:

Method Rule 4.5 -4.5 4.2 -4.7
Math.round nearest; .5 rounds up 5 -4 4 -5
Math.floor down (toward −∞) 4 -5 4 -5
Math.ceil up (toward +∞) 5 -4 5 -4
Math.trunc drop decimals (toward 0) 4 -4 4 -4
js · try it yourself
for (const x of [4.5, -4.5, 4.2, -4.7]) {
  console.log(x, Math.round(x), Math.floor(x), Math.ceil(x), Math.trunc(x));
}

// Round to 2 decimal places:
const price = 19.4567;
console.log(Math.round(price * 100) / 100);
4.5 5 4 5 4
-4.5 -4 -5 -4 -4
4.2 4 4 5 4
-4.7 -5 -5 -4 -4
19.46

Powers, roots and signs

js · try it yourself
console.log(Math.pow(2, 10), 2 ** 10);
console.log(Math.sqrt(144), Math.cbrt(27));
console.log(Math.abs(-7.5));
console.log(Math.sign(-3), Math.sign(0), Math.sign(8));
console.log(Math.hypot(3, 4));       // length of the diagonal: √(3² + 4²)
1024 1024
12 3
7.5
-1 0 1
5

Minimum and maximum

Math.min and Math.max take any number of arguments. Spread an array into them with .... A common use is clamping a value into a range:

js · try it yourself
const scores = [72, 95, 88, 61];
console.log(Math.max(...scores), Math.min(...scores));

const clamp = (value, lo, hi) => Math.min(Math.max(value, lo), hi);
console.log(clamp(150, 0, 100), clamp(-5, 0, 100), clamp(42, 0, 100));
console.log(Math.max());   // no arguments
95 61
100 0 42
-Infinity

Trigonometry and logarithms

Trigonometric functions work in radians, not degrees. Convert with degrees * Math.PI / 180:

js · try it yourself
const toRad = (deg) => (deg * Math.PI) / 180;
console.log(Math.sin(toRad(90)));
console.log(Math.cos(toRad(60)).toFixed(4));
console.log(Math.log10(1000), Math.log2(8));
console.log(Math.log(Math.E));
1
0.5000
3 3
1

Random numbers

Math.random() returns a pseudo-random decimal from 0 (inclusive) up to 1 (exclusive). Combine it with Math.floor to get whole numbers in a range. Because the result is different on every run, this example isn't a playground — copy it into one and run it a few times to see:

js
// A decimal in [0, 1)
Math.random();

// A whole number from min to max (both inclusive)
function randomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

randomInt(1, 6);              // a dice roll: 1, 2, 3, 4, 5 or 6

// Pick a random element
const colors = ["red", "green", "blue"];
colors[Math.floor(Math.random() * colors.length)];

Warning: Math.random() is not secure. For passwords, tokens or anything security-related, use crypto.getRandomValues() or crypto.randomUUID().

Quick reference

Method Returns
Math.round/floor/ceil/trunc(x) rounded integer
Math.abs(x) absolute value
Math.sign(x) -1, 0 or 1
Math.pow(x, y) / x ** y x to the power y
Math.sqrt(x) / Math.cbrt(x) square / cube root
Math.min(...) / Math.max(...) smallest / largest argument
Math.sin/cos/tan(rad) trigonometry
Math.log/log2/log10(x) logarithms
Math.random() pseudo-random number in [0, 1)

Test yourself

What is Math.floor(-2.3)?

  1. -2
  2. -3
  3. 2
  4. -2.3

Which expression returns a whole number from 1 to 10 inclusive?

  1. Math.random() * 10
  2. Math.floor(Math.random() * 10) + 1
  3. Math.round(Math.random() * 10)
  4. Math.ceil(Math.random() * 11)

How do you find the largest value in the array nums?

  1. Math.max(nums)
  2. Math.max(...nums)
  3. nums.max()
  4. Math.largest(nums)