The Math object in JavaScript provides properties and methods for mathematical constants and functions. It is a built-in object and is not a constructor, so you do not create instances of it.

💡 Key Feature
All methods and properties of Math are static, so you call them directly on Math without instantiating it.
Commonly Used Properties
| Property | Description |
|---|---|
Math.PI | Ratio of the circumference of a circle to its diameter (~3.14159) |
Math.E | Euler's number, base of natural logarithms (~2.718) |
Math.SQRT2 | Square root of 2 (~1.414) |
Math.LN10 | Natural logarithm of 10 (~2.302) |
Essential Math Methods
Math.abs(x)- Returns the absolute value ofx.Math.round(x)- Roundsxto the nearest integer.Math.floor(x)- Roundsxdown to the nearest integer.Math.ceil(x)- Roundsxup to the nearest integer.Math.max(...values)- Returns the largest of the given numbers.Math.min(...values)- Returns the smallest of the given numbers.Math.random()- Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).Math.sqrt(x)- Returns the square root ofx.Math.pow(base, exponent)- Returnsbaseraised to the power ofexponent.
💡 Using Math.random()
To generate a random integer between two values min and max (inclusive):
📌 Deep Dive: Random Integer Between min and max
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Method Behavior Comparison
| Method | Input | Output |
|---|---|---|
Math.floor(4.9) | 4.9 | 4 |
Math.ceil(4.1) | 4.1 | 5 |
Math.round(4.5) | 4.5 | 5 |
Math.round(4.4) | 4.4 | 4 |
⚠️ Important
Since Math.random() returns a floating-point number in [0, 1), using Math.floor with multiplication is necessary to convert it into an integer range.
Additional Useful Methods
Math.trunc(x)- Removes the decimal part ofx, effectively truncating toward zero.Math.sign(x)- Returns1if positive,-1if negative, and0if zero.Math.log(x)- Natural logarithm (base e) ofx.Math.exp(x)- Returnseraised to the power ofx.
💡 Remember
All Math methods are deterministic except Math.random(), which generates pseudo-random values.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which method would you use to round a number down to the nearest integer?
Question 2 of 2
What does Math.random() return?
Loading results...