Javascript Math (Math Object)


The Math object is used for common mathematical calculations in JavaScript.

Unlike String and Date objects, the Math object is not a class of an object. There is no constructor Math(), so you can use a Math object directly without creating a Math object. The method inside the Math object is also a static method, which can be used directly through Math.function.

Note

For ordinary addition, subtraction and other operations, please refer to JavaScript operator.


Math object property

JavaScript provides eight Math object properties that represent some of the commonly used arithmetic values:

Properties Description
Math.E Arithmetic constant e, the base of the natural logarithm(approximately equal to 2.718)
Math.LN2 The natural logarithm of 2(approximately equal to 0.693)
Math.LN10 The natural logarithm of 10(approximately equal to 2.302)
Math.LOG2E The logarithm of e based on 2(approximately equal to 1.414)
Math.LOG10E base 10 logarithm of e(approximately equal to 0.434)
Math.PI pi(approximately equal to 3.14159)
Math.SQRT1_2 The reciprocal of the square root of 2(approximately equal to 0.707)
Math.SQRT2 2 square root(approximately equal to 1.414)

Use the properties of Math

The above properties of the Math object can be used directly:

var pi = Math.PI;
document.write(pi);
Output
3.141592653589793

The Math object method

The Math object also provides the following methods for dealing with common mathematical calculations:

Method Description
Math.abs gives the absolute value of the number
Math.acos gets the number of arc cosine values ​​
Math.asin gives the arc sine of the number
Math.atan gets the arctangent of the number
Math.atan2(y, x) Angle value from x-axis to point(x, y)(between -PI and PI radians)
Math.ceil Rounded up(greater than or equal to the current number of integers), and floor() the opposite
Math.cos gets the cosine of the number
Math.exp(x) gets the x power of e
Math.floor Rounded down(integers less than or equal to the current number), as opposed to ceil()
Math.log gets the natural logarithm of the number(at the end e)
Math.max Get the maximum number of arguments, as opposed to min()
Math.min Get the smallest number of arguments, as opposed to max()
Math.pow(x, y) to get the value of the power of x at power y
Math.random Get a random number between 0 and 1
Math.round rounded to the nearest rounding rule
Math.sin Gets the sine of a number
Math.sqrt Get the square root of a number
Math.tan gets the tangent of a number



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook