← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 2 of 26

JS Math

Think of a scientific calculator built right into your programming language, always available, never needing a separate app, ready to round numbers, find the biggest value, or generate a random pick. JavaScript's built-in Math object is exactly that calculator, a collection of ready-made methods for common numeric tasks so you never have to write rounding or randomization logic completely from scratch. From displaying a rounded quiz score to picking a random featured tutorial on cookiescursor.com, the Math object quietly handles a lot of everyday numeric work.

Rounding: round, floor, and ceil

Math.round() rounds to the nearest whole number, Math.floor() always rounds down, and Math.ceil() always rounds up, regardless of how close the decimal is to the next whole number.

Note: Use Math.floor() when calculating something like 'how many complete groups fit', since it never rounds up past what actually fits.

Warning: Math.round() rounds exactly .5 upward, which can be surprising for negative numbers, where it behaves slightly differently than expected.

Example: Rounding: round, floor, and ceil

javascript
console.log(Math.round(4.5));
console.log(Math.floor(4.9));
console.log(Math.ceil(4.1));

abs, max, and min

Math.abs() returns a number's absolute value, always positive, Math.max() returns the largest of a set of values, and Math.min() returns the smallest. All three accept any number of arguments and are commonly combined with the spread operator to work on array contents.

Note: Use the spread operator with an array, like Math.max(...scores), to find the maximum value directly from an array of numbers.

Warning: Math.max() and Math.min() called with zero arguments return -Infinity and Infinity respectively, which can silently break comparisons on an empty list.

Example: abs, max, and min

javascript
console.log(Math.abs(-5));
console.log(Math.max(3, 7, 2));
console.log(Math.min(3, 7, 2));

Math.random()

Math.random() returns a random decimal number between 0 (inclusive) and 1 (exclusive), which is typically scaled and rounded to produce a random whole number within a specific range.

Note: The common formula Math.floor(Math.random() * (max - min + 1)) + min reliably generates a random whole number between min and max, inclusive.

Warning: Math.random() is not cryptographically secure, avoid it for anything security sensitive, like generating tokens or passwords.

Example: Math.random()

javascript
console.log(Math.random()); // decimal between 0 and 1
const diceRoll = Math.floor(Math.random() * 6) + 1;
console.log(diceRoll);

pow and sqrt

Math.pow(base, exponent) raises a number to a power, functionally equivalent to the ** operator, and Math.sqrt() calculates a number's square root. Math.sqrt() of a negative number returns NaN, since JavaScript's Math object only works with real numbers.

Note: In modern code, the ** operator is generally preferred over Math.pow() for its more concise syntax, though both produce identical results.

Warning: Math.sqrt() of a negative number returns NaN, not an error, so it's worth checking a value's sign before calling it if negatives are possible.

Example: pow and sqrt

javascript
console.log(Math.pow(2, 3));
console.log(Math.sqrt(16));
console.log(Math.sqrt(-4)); // NaN

Math.PI and Other Constants

Math.PI provides the mathematical constant pi with full floating-point precision, useful for geometry calculations like circle area or circumference, without needing to type or remember its digits manually.

Note: Using Math.PI instead of hardcoding an approximation like 3.14 ensures your calculations remain as precise as JavaScript's number type allows.

Warning: Math.PI is a read-only property, not a function, it's accessed without parentheses, Math.PI, not Math.PI().

Example: Math.PI and Other Constants

javascript
console.log(Math.PI);
const radius = 5;
console.log(Math.PI * radius * radius); // circle area
Common Mistakes
  1. Confusing Math.round(), Math.floor(), and Math.ceil(), they round differently, to nearest, down, and up respectively.
  2. Forgetting Math.random() returns a decimal between 0 and 1, requiring extra math to scale it into a useful range.
  3. Assuming Math.max() or Math.min() work directly on an array, they require individual arguments or the spread operator.
Chapter Summary
  • Math.round, Math.floor, and Math.ceil round numbers to the nearest, lower, or upper whole number respectively.
  • Math.abs returns a positive version of a number, and Math.max/Math.min find the largest or smallest of a set of values.
  • Math.random generates a random decimal, and Math.pow/Math.sqrt handle powers and square roots, alongside the constant Math.PI.
Browser Support

The Math object and all its standard methods are core JavaScript features supported identically in every browser.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.