Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Math object

JavaScript Math object


Math object

The Math object is used to perform mathematical tasks.

The Math object, unlike Date and String, is not a class of objects, so there is no constructor Math().

Syntax

var x = Math.PI; // 返回 PI
var y = Math.sqrt(16); // 返回 16 的平方根

For the Math object tutorial, please refer to this site'sJavaScript Math object tutorial.


Math object properties

property Description
E Returns the arithmetic constant e, the base of the natural logarithm (approximately 2.718).
LN2 Return the natural logarithm of 2 (approximately 0.693).
LN10 Return the natural logarithm of 10 (approximately 2.302).
LOG2E Returns the logarithm of e to the base 2 (approximately 1.4426950408889634).
LOG10E Return the base-10 logarithm of e (approximately 0.434).
PI Returns pi (approximately 3.14159).
SQRT1_2 Returns the reciprocal of the square root of 2 (approximately 0.707).
SQRT2 Returns the square root of 2 (approximately 1.414).

Math object methods

Method Description
abs(x) Returns the absolute value of x.
acos(x) Returns the arccosine of x.
asin(x) Returns the arcsine of x.
atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians.
atan2(y,x) Returns the angle from the x-axis to the point (x,y) (between -PI/2 and PI/2 radians).
ceil(x) Rounds a number up.
cos(x) Returns the cosine of a number.
exp(x) Returns E to the power of x.
floor(x) Rounds x down.
log(x) Returns the natural logarithm (base e) of a number.
max(x,y,z,...,n) Returns the highest value among x,y,z,...,n.
min(x,y,z,...,n) Returns the lowest value among x,y,z,...,n.
pow(x,y) Returns x raised to the power of y.
random() Returns a random number between 0 and 1.
round(x) Rounds to the nearest integer.
sin(x) Returns the sine of a number.
sqrt(x) Returns the square root of a number.
tan(x) Returns the tangent of an angle.
tanh(x) Returns the hyperbolic tangent of a number.
trunc(x) Removes the fractional part of a number, keeping only the integer part.