// Math.abs(x) returns the absolute (positive) value of x
Math.abs(-3.14); // Returns 3.14
// Math.ceil(x) returns the value of x rounded up to its nearest integer
Math.ceil(3.14); // Returns 4
// Math.cos(x) returns the cosine (a value between -1 and 1) of the angle x (given in radians).
// Convert degrees to radians if you want to use degrees instead of radians, like this:
// Angle in radians = Angle in degrees x PI / 180.
Math.cos(0 * Math.PI / 180); // Returns 1 (the cos of 0 degrees)
// Math.floor(x) returns the value of x rounded down to its nearest integer
Math.floor(3.14); // Returns 3
// Math.max() is used to find the highest value in a list of arguments
Math.max([x[, y[, �]]])
Math.min(920, 70, 30, 0, -15, -5); // Returns 920
// Math.min() is used to find the lowest value in a list of arguments
Math.min(920, 70, 30, 0, -15, -5); // Returns -15
// Math.pow(x, y) returns the value of x to the power of y
Math.pow(5, 2) // Returns 25
// Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):
Math.random(); // Returns a random number
// Math.round(x) returns the value of x rounded to its nearest integer
Math.round(3.14); // Returns 3
Math.round(3.7); // Returns 4
// Math.sin(x) returns the sine (a value between -1 and 1) of the angle x (given in radians).
// Convert degrees to radians if you want to use degrees instead of radians, like this:
// Angle in radians = Angle in degrees x PI / 180.
Math.sin(90 * Math.PI / 180); // Returns 1 (the sine of 90 degrees)
// Math.sqrt(x) returns the square root of x
Math.sqrt(25); // Returns 5
Note that the trigonometric functions (sin()
, cos()
, tan()
, asin()
, acos()
, atan()
, atan2()
) expect or return angles in radians. To convert radians to degrees, divide by (Math.PI / 180)
, and multiply by this to convert the other way.
Note that many math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.