JavaScript Math object
reviewing this calculation-oriented built-in object
// updated 2025-05-11 11:43
The Math object built into JavaScript provides us with several mathematical functions so we don't have to write them ourselves (imagine re-inventing sines and cosines from scratch!)
Some common functions include:
Math.random()- gives us a random decimal number between 0 and 1 inclusive
Math.abs(someNumberOrCalculation)- gives us the absolute value of a number or a calcuation, e.g.
-1becomes12just remains2-3+2becomes1
- gives us the absolute value of a number or a calcuation, e.g.
Math.floor(someNumber)- gives us the integer that is less than or equal to some number, e.g.
12.24becomes12-1.37becomes-20becomes0
- gives us the integer that is less than or equal to some number, e.g.
Math.ceil(someNumber)- gives us the integer that is greater than or equal to some number, e.g.
12.24becomes13-1.37becomes-10becomes0
- gives us the integer that is greater than or equal to some number, e.g.
Math.sqrt(x)- gives us the square root of x
Math.pow(x, y)- gives us the "x to the power of y"
Math.sin(x)- gives us the sine of x (in radians)
Math.cos(x)- gives us the cosine of x (in radians)
Math.tan(x)- gives us the tangent of x (in radians)
Math.asin(x)+Math.acos(x)+Math.atan(x)- gives us the arcsine + arccosine + arctangent of x (in radians)
Math.PI- gives us the approximation of pi (3.14159)
Math.E- gives us the approximation to Euler's constant (2.718)
Further exploration
- Math object in Mozilla Developer Docs (with more examples and functions)