Properties and Methods of Math Object
In JavaScript, the Math
object is a built-in object that provides mathematical constants and functions. It is not a constructor and its properties and methods are all static. This means that you do not need to create an instance of the Math
object to use its methods and properties.
Here are some of the methods and properties of the Math
object:
- Properties:
Math.PI
: returns the value of pi (3.141592653589793)Math.E
: returns the value of e (2.718281828459045)Math.LN2
: returns the natural logarithm of 2 (0.6931471805599453)Math.LN10
: returns the natural logarithm of 10 (2.302585092994046)
- Methods:
Math.abs(x)
: returns the absolute value of xMath.ceil(x)
: returns the smallest integer greater than or equal to xMath.floor(x)
: returns the largest integer less than or equal to xMath.max(x1, x2, ..., xn)
: returns the largest of the specified valuesMath.min(x1, x2, ..., xn)
: returns the smallest of the specified valuesMath.pow(x, y)
: returns the value of x to the power of yMath.sqrt(x)
: returns the square root of xMath.round(x)
: returns the value of x rounded to the nearest integerMath.random()
: returns a random number between 0 and 1Math.sin(x)
: returns the sine of xMath.cos(x)
: returns the cosine of xMath.tan(x)
: returns the tangent of xMath.asin(x)
: returns the arcsine of xMath.acos(x)
: returns the arccosine of xMath.atan(x)
: returns the arctangent of xMath.log(x)
: returns the natural logarithm (base e) of xMath.exp(x)
: returns the value of e to the power of xMath.atan2(y, x)
: returns the angle between the positive x-axis and the point (x,y)
Here's an example of how to use some of the methods of the Math
object:
console.log(Math.PI); // Output: 3.141592653589793
console.log(Math.sqrt(25)); // Output: 5
console.log(Math.random()); // Output: a random number between 0 and 1
The Math
object is a useful tool for performing mathematical operations in JavaScript, and can help to simplify complex calculations.