What is Math Object in JavaScript Program?

JavascriptWeb DevelopmentFront End Technology

Math is an inbuilt object provided by JavaScript that has multiple attributes and methods for different mathematical functions and constants. This is not a function object.

  • Maths objects generally work with the Number type.

  • The Math object does not have a constructor and all the properties and methods are fixed or static.

  • The cosine function is also known as Math.cos(y). Similarly, the pi constant is known as Math.PI.

All the properties or methods provided by the Math object are static and can be called by using the Math as an object without actually creating it. In this article, we will explore different available methods and properties that are used in JavaScript.

Some static Math properties are listed below −

PropertyDescriptionReturn value
Math.EThis represents the Euler’s constant. Its default value is 2.718 approximately.Euler’s number
Math.LN2This represents the natural log which has an approx value of 0.693147180.Natural Log of 2
Math.LN10This represents the natural log with Base 10 which has an approx value of 2.302585.Natural Log of 10
Math.LOG2EThis represent Base-2 log of E with approx value of 1.442695.Base 2 Log of E
Math.LOG10EThis represent Base-10 log of E with approx value of 0.43429844Base 10 Log of E
Math.PIThis represents the ratio of the circumference of a circle to its diameter i.e. 3.14159.PI value
Math.SQRT1_2This represents the square root of 1/2 with approx value of 0.70710678.The square root of 1/2
Math.SQRT2This represents square root of 2 which is 1.41421356 approximatelyThe square root of 2

Example 1

In the example below, we apply different static Math properties.

# index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8" />
   <meta http-equiv="X-UA-Compatible"
   content="IE=edge" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>Document</title>
</head>
<h1 style="color: red;">
   Welcome To Tutorials Point
</h1>
<body>
   <script>
      console.log("Math.PI", Math.PI);
      console.log("Math.SQRT2", Math.SQRT2);
      console.log("Math.SQRT1_2", Math.SQRT1_2);
      console.log("Math.LN10", Math.LN10);
      console.log("Math.LN2", Math.LN2);
      console.log("Math.LOG10E", Math.LOG10E);
      console.log("Math.LOG2E", Math.LOG2E);
   </script>
</body>
</html>

Output

It will produce the following output in the Console. Math.PI

Math.PI 3.141592653589793
Math.SQRT2 1.4142135623730951
Math.SQRT1_2 0.7071067811865476
Math.LN10 2.302585092994046
Math.LN2 0.6931471805599453
Math.LOG10E 0.4342944819032518
Math.LOG2E 1.4426950408889634
Math.LOG2E 1.4426950408889634

Static Math Methods: Math objects have static methods associated with them. Some of the methods are described below.

Sr.NoMethods & Description
1Math.abs(y)
This returns the positive value of y
2Math.acos(y)
This returns the acos value of y
3Math.acosh(y)
Returns hyperbolic a-cosine value of y
4Math.asin(y)
Returns arcsine value of y
5Math.asinh(y)
Returns hyperbolic arcsine value of y
6Math.atan(y)
Returns arctangent value of y
7Math.atanh(y)
Returns hyperbolic arctangent value of y
8Math.atan2(y, x)
Returns arctangent of the quotient of its arguments
9Math.cbrt(y)
Returns the cube root of y.
10Math.ceil(y)
Returns the smallest integer greater than or equal to y.
11Math.clz32(y)
Returns the number of leading zero bits of the 32-bit integer y.
12Math.cos(y)
Returns the cosine of the angle y.

Example 2

In the below example we apply different Math methods such as ceil, floor, round, sin, cos, min, max, etc.

# index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8" />
   <meta http-equiv="X-UA-Compatible"
   content="IE=edge" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>Document</title>
</head>
<body>
   <h1 style="color: red;">
      Welcome To Tutorials Point
   </h1>
   <script>
      console.log("ceil ", Math.ceil(9.6));
      console.log("floor ", Math.floor(9.6));
      console.log("round ", Math.round(9.6));
      console.log("sine ", Math.sin(30));
      console.log("cosine ", Math.cos(30));
      console.log("min ", Math.min(30, 40));
      console.log("max ", Math.max(30, 40));
      console.log("sign ", Math.sign(-40));
      console.log("abs ", Math.sign(-40));
   </script>
</body>
</html>

Output

It will produce the following output in the Console.

ceil 10
floor 9
round 10
sine -0.9880316240928618
cosine 0.15425144988758405
min 30
max 40
sign -1
abs -1
raja
Updated on 28-Apr-2022 14:03:36

Advertisements