The JavaScript Math Object
The built-in Math object is a namespace containing mathematical constants and functions. Unlike most JavaScript objects, you never instantiate it — all properties and methods are accessed directly on Math. It provides everything from basic rounding to trigonometry and logarithms.
Rounding Methods
Math.round(x) rounds to the nearest integer (0.5 rounds up). Math.floor(x) always rounds down toward negative infinity. Math.ceil(x) always rounds up toward positive infinity. Math.trunc(x) removes the decimal part without rounding.
Math.random()
Math.random() returns a pseudo-random float in the range [0, 1). To generate a random integer between min and max (inclusive), use the formula Math.floor(Math.random() * (max - min + 1)) + min.
Other Useful Methods
Math.abs(x)— absolute valueMath.max(...values)andMath.min(...values)— largest/smallestMath.pow(base, exp)— exponentiation (prefer the**operator)Math.sqrt(x)— square root