By the end of this lesson, you will be able to use JavaScript's built-in Math object to access mathematical constants and perform common calculations such as rounding, random number generation, maximum/minimum, and powers.
What it is
Math is a built-in global object in JavaScript. It is not a constructor; you do not create instances with new Math(). Instead, it is a namespace that contains static properties and methods. Constants such as Math.PI and Math.E are properties. Functions such as Math.max(), Math.floor(), and Math.random() are methods.
Mental model: think of Math as a toolbox of ready-made mathematical helpers. Related terms include ECMAScript, IEEE 754 double-precision floating-point numbers, and numeric methods on the Number object.
Why it matters
- It provides standard constants like
Math.PIfor geometry and trigonometry. - It offers rounding helpers such as
Math.floor(),Math.ceil(),Math.round(), andMath.trunc(). - It generates pseudo-random numbers with
Math.random(), useful for games, sampling, and UI effects. - It compares values with
Math.max()andMath.min(), useful for validation and clamping. - It performs powers and roots with
Math.pow(),Math.sqrt(), andMath.cbrt().
Syntax or steps
Use Math.CONSTANT for values and Math.method(arguments) for calculations.
Math.max(3, 7); // 7
Math.PI
Math.floor(4.9)
Math.random()
Most methods accept numbers and return numbers. If an argument cannot be converted to a number, the result is usually NaN.
Example
const scores = [82, 91, 76, 88];
const highest = Math.max(...scores);
const lowest = Math.min(...scores);
const average = scores.reduce((sum, score) => sum + score, 0) / scores.length;
const randomStudent = Math.floor(Math.random() * scores.length);
const distance = Math.hypot(3, 4);
console.log(highest);
console.log(lowest);
console.log(average);
console.log(randomStudent);
console.log(distance);
Part-by-part:
Math.max(...scores)spreads the array into separate arguments, returning91.Math.min(...scores)returns76.averageusesreduce()and division;Mathis not needed for basic arithmetic.Math.random()returns a number from0up to but not including1. Multiplying byscores.lengthand usingMath.floor()gives a valid array index.Math.hypot(3, 4)returns the square root of3squared plus4squared, which is5.
Common mistakes
- Using
new Math(). Fix: call methods directly, such asMath.random(). - Passing an array directly to
Math.max(). Fix: use spread syntax,Math.max(...numbers), orreduce(). - Assuming
Math.random()can return1. Fix: remember the upper bound is exclusive; useMath.floor(Math.random() * max)for integers from0tomax - 1. - Expecting exact decimal arithmetic. Fix: remember JavaScript numbers are floating-point; for money or exact decimals, use integer cents or a decimal library.
When to use it
Use Math for mathematical operations and constants. Use Number methods for parsing and formatting numeric strings. Use a decimal library when exact decimal arithmetic is required.
| Need | Use | Example |
|---|---|---|
| Constants and math helpers | Math |
Math.PI, Math.sqrt(16) |
| Convert or inspect numbers | Number |
Number("42"), Number.isInteger(42) |
| Exact decimal calculations | Decimal library | decimal.js or big.js |
Practice
Guided exercise: Write a function that returns a random integer between 1 and 6, like a die roll.
function rollDie() {
return Math.floor(Math.random() * 6) + 1;
}
Expected output: a number from 1 to 6.
Challenge: Clamp a value between 0 and 100 using Math.max() and Math.min().
function clamp(value) {
return Math.min(100, Math.max(0, value));
}
Solution hint: apply Math.max() first to enforce the lower bound, then Math.min() to enforce the upper bound.
Quick check
Question: What does Math.floor(Math.random() * 10) return?
Answer: An integer from 0 to 9, because Math.random() returns a number greater than or equal to 0 and less than 1.
Summary
The Math object is JavaScript's built-in toolbox for constants and common numeric operations. It is accessed statically, not instantiated, and is best used for standard math helpers while Number methods handle parsing and formatting.