Math object in JavaScript

In this tutorial, we will understand how to use Math Object in JavaScript.

Math Object:

It is used to perform simple and complex arithmetic operations.

There is no need of object creation for Math object. We can access the methods directly like Math.methodname(). Since Math object is static.

Properties of Math Object:

  • E  : holds Euler number whose approximate value is 2.718.
  • LN2 : holds natural logarithm of 2, whose approximate value is 0.693.
  • LN10 : holds natural logarithm of 10, whose approximate value is 2302.
  • LOG2E : holds the base-2 logarithm of E, whose approximate value is 1.442.
  • LOG10E : holds the base-10 logarithm of E, whose approximate value is 0.434.
  • PI : holds the numerical value of PI, whose approximate value is 3.142.
  • SQRT1_2 : holds the square root of 1/2, whose approximate value is 0.707.
  • SQRT2 : holds the square root of 2, whose approximate value is 1.414.

Methods of Math Object:

  • abs(x) : returns absolute value of x.
  • acos(x) : returns arccosine ( in radians ) of x.
  • asin(x) : returns arcsine ( in radians ) of x.
  • atan(x): returns arctangent of x with numerical value between -PI/2 and PI/2.
  • atan2(y,x) : returns the arctangent of the quotient on dividing y and x.
  • ceil(x) : rounds up x to the nearest bigger integer.
  • cos(x) : returns cosine value of x.
  • exp(x) : returns the value of e power x.
  • floor(x) : rounds up x to the nearest smaller integer.
  • log(x) : returns the natural logarithmic value of x.
  • max(x,y,z,…n) : returns the highest from the given list.
  • min(x,y,z,…n) : returns the lowest from the given list.
  • pow(x,y) : returns x to the power of y.
  • random() : returns a random number between 0 and 1.
  • round(x) : rounds up x to the nearest integer.
  • sin(x) : returns the sine value of x.
  • sqrt(x) : returns the square root of x.
  • tan(x) : returns the tangent value of x.

Let us see the example

console.log("floor : "+Math.floor(12.6));
console.log("log : "+Math.log(12.6));
console.log("max : "+Math.max(12,5,8,9));
console.log("min : "+Math.min(12,5,8,9));
console.log("pow : "+Math.pow(2,3));
console.log("random : "+Math.random());
console.log("round: "+Math.round(12.6));
console.log("sin : "+Math.sin(45));
console.log("sqrt : "+Math.sqrt(16));
console.log("tan : "+Math.tan(45));
console.log("exp : "+Math.exp(10));

Output:

math object in JavaScript

In this way, we can use the Math object.

 

Leave a Reply

Your email address will not be published. Required fields are marked *