JavaScript Math
- Previous Page JS Date Setting Method
- Next Page JS Random
The JavaScript Math object allows you to perform mathematical tasks on numbers.
Math.round()
Math.round(x)
The returned value is x rounded to the nearest integer:
Example
Math.round(6.8); // Returns 7 Math.round(2.3); // Returns 2
Math.pow()
Math.pow(x, y)
The returned value is x to the power of y:
Example
Math.pow(8, 2); // Returns 64
Math.sqrt()
Math.sqrt(x)
Returns the square root of x:
Example
Math.sqrt(64); // Returns 8
Math.abs()
Math.abs(x)
Returns the absolute (positive) value of x:
Example
Math.abs(-4.7); // Returns 4.7
Math.ceil()
Math.ceil(x)
The returned value is x CeilingThe closest integer:
Example
Math.ceil(6.4); // Returns 7
Math.floor()
Math.floor(x)
The returned value is x FlooringThe closest integer:
Example
Math.floor(2.7); // Returns 2
Math.sin()
Math.sin(x)
Returns the sine of angle x (a value between -1 and 1).
If you want to use degrees instead of radians, you need to convert the angle to radians:
Angle in radians = Angle in degrees x PI / 180.
Example
Math.sin(90 * Math.PI / 180); // Returns 1 (the sine of 90 degrees)
Math.cos()
Math.cos(x)
Returns the cosine of angle x (in radians) (a value between -1 and 1).
If you want to use degrees instead of radians, you need to convert the angle to radians:
Angle in radians = Angle in degrees x PI / 180.
Example
Math.cos(0 * Math.PI / 180); // Returns 1 (cosine of 0 degrees)
Math.min() and Math.max()
Math.min()
and Math.max()
Can be used to find the lowest or highest value in the parameter list:
Example
Math.min(0, 450, 35, 10, -8, -300, -78); // Returns -300
Example
Math.max(0, 450, 35, 10, -8, -300, -78); // Returns 450
Math.random()
Math.random()
Returns a random number between 0 (inclusive) and 1 (exclusive):
Example
Math.random(); // Returns a random number
You will learn more about Math.random()
knowledge.
Math properties (constants)
JavaScript provides 8 mathematical constants accessible via the Math object:
Example
Math.E // Returns Euler's number (Euler's number) Math.PI // Returns the value of pi (PI) Math.SQRT2 // Returns the square root of 2 Math.SQRT1_2 // Returns the square root of 1/2 Math.LN2 // Returns the natural logarithm of 2 Math.LN10 // Returns the natural logarithm of 10 Math.LOG2E // Returns the logarithm of e to the base 2 (approximately equal to 1.414) Math.LOG10E // Returns the logarithm of e to the base 10 (approximately equal to 0.434)
Math constructor
Unlike other global objects, the Math object does not have a constructor. Methods and properties are static.
All methods and properties (constants) can be used without first creating a Math object.
Math object method
Method | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x, in radians |
asin(x) | Return the arcsine of x, in radians |
atan(x) | Return the arctangent of x, in radians, with a value between -PI/2 and PI/2 radians. |
atan2(y,x) | Return the angle from the x-axis to the point (x,y) |
ceil(x) | Round up x |
cos(x) | Return the cosine of x |
exp(x) | Return the value of Ex |
floor(x) | Round down x |
log(x) | Return the natural logarithm of x (base e) |
max(x,y,z,...,n) | Return the maximum value |
min(x,y,z,...,n) | Return the minimum value |
pow(x,y) | Return x to the power of y |
random() | Return a random number between 0 and 1 |
round(x) | Round x to the nearest integer |
sin(x) | Return the sine of x (x in degrees) |
sqrt(x) | Return the square root of x |
tan(x) | Return the tangent of an angle |
Complete Math Reference Manual
For a complete reference manual, please visit our full Math Object Reference Manual.
This reference manual includes descriptions and examples of all Math properties and methods.
- Previous Page JS Date Setting Method
- Next Page JS Random