• JavaScript Video Tutorials

JavaScript - The Math Object



The math object provides you properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it.

Thus, you refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method's argument.

Syntax

The syntax to call the properties and methods of Math are as follows

var pi_val = Math.PI; // Property
var sine_val = Math.sin(30); // Method

Math Properties

Here is a list of all the properties of Math and their description.

Sr.No. Property & Description
1 E

Euler's constant and the base of natural logarithms, approximately 2.718.

2 LN2

Natural logarithm of 2, approximately 0.693.

3 LN10

Natural logarithm of 10, approximately 2.302.

4 LOG2E

Base 2 logarithm of E, approximately 1.442.

5 LOG10E

Base 10 logarithm of E, approximately 0.434.

6 PI

Ratio of the circumference of a circle to its diameter, approximately 3.14159.

7 SQRT1_2

Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707.

8 SQRT2

Square root of 2, approximately 1.414.

In the following sections, we will have a few examples to demonstrate the usage of Math properties.

Math Methods

Here is a list of the methods associated with Math object and their description

Sr.No. Method & Description
1 abs()

Returns the absolute value of a number.

2 acos()

Returns the arccosine (in radians) of a number.

3

acosh()

Returns the inverse hyperbolic consine of a number.

4 asin()

Returns the arcsine (in radians) of a number.

5

asinh()

Returns the inverse hyperbolic sine of a number.

6 atan()

Returns the arctangent (in radians) of a number.

7 atan2()

Returns the arctangent of the quotient of its arguments.

8

atanh()

Returns the inverse hyperbolic tangent of a number.

9

cbrt()

Finds a cube root of a given number.

10 ceil()

Returns the smallest integer greater than or equal to a number.

11

clz32()

Returns the number of leading zero in 32-bit binary number.

12 cos()

Returns the cosine of a number.

13

cosh()

It returns the hyperbolic cosine of a number.

14 exp()

Returns EN, where N is the argument, and E is Euler's constant, the base of the natural logarithm.

15

expm1()

Returns EN - 1, where N is the argument, and E is Euler's constant, the base of the natural logarithm.

16 floor()

Returns the largest integer less than or equal to a number.

17

fround()

Returns a nearest 32-bit single precision float representation of the number.

18

hypot()

Calculates the square root of the sum of squares of arguments.

19

imul()

Calculates the 32-bit multiplication of parameters.

20 log()

Returns the natural logarithm (base E) of a number.

21

log10()

Returns the logarithm (base 10) of a number.

22

log1p()

Return the natural logarithm (base E) of 1 + N, where N is an argument.

23

log2()

Returns the base 2 logrithm of a number.

24 max()

Returns the largest of zero or more numbers.

25 min()

Returns the smallest of zero or more numbers.

26 pow()

Returns base to the exponent power that is, base exponent.

27 random()

Returns a pseudo-random number between 0 and 1.

28 round()

Returns the value of a number rounded to the nearest integer.

29

sign()

Return -1 or 1 indicating the sign of the number.

30 sin()

Returns the sine of a number.

31

sinh()

Return the hyperbolic sin.

32 sqrt()

Returns the square root of a number.

33 tan()

Returns the tangent of a number.

34

tanh()

Returns the hyperbolic tangent of the number.

35 trunc()

Returns the integer part of the number.

In the following sections, we will have a few examples to demonstrate the usage of the methods associated with Math.

Examples

Example: Math Properties

The example below demonstrates that each property of the Math object has a constant value.

Here, we have accessed the values of the 'E', 'LN2’, and 'PI', etc., properties.

<html>
<head>
   <title> JavaScript - Math object's properties </title>
</head>
<body>
   <p id = "output"> </p>
   <script>
      document.getElementById("output").innerHTML = 
      "Math.E == " + Math.E + "<br>" +
      "Math.LN2 == " + Math.LN2 + "<br>" +
      "Math.LN10 == " + Math.LN10 + "<br>" +
      "Math.PI == " + Math.PI + "<br>"+ 
      "Math.LOG2E == " + Math.LOG2E + "<br>" + 
      "Math.LOG10E == " + Math.LOG10E;
   </script>
</body>
</html>

Output

After executing the above program, it returns the values of the provided Math properties.

Math.E == 2.718281828459045
Math.LN2 == 0.6931471805599453
Math.LN10 == 2.302585092994046
Math.PI == 3.141592653589793
Math.LOG2E == 1.4426950408889634
Math.LOG10E == 0.4342944819032518

Example: Math ceil() method

Here, we are computing the JavaScript ceil() method to return the smallest larger integer value than the number passed as an argument. Here, the method returns 6 for the 5.9 value.

<html>
<head>
   <title> JavaScript - Math.ceil() method </title>
</head>
<body>
   <p id = "output"> </p>
   <script>
      let ans = Math.ceil(5.9);
      document.getElementById("output").innerHTML = 
      "Math.ceil(5.9) = " + ans;
   </script>
</body>
</html>

Output

After executing the above program, it returns the result as 6.

Math.ceil(5.9) = 6

Example: Math max() method

The Math.max() method is used to get the maximum value among the arguments passed as an array.

Here, we have passed six arguments to the Math.max() object, and the method returns the maximum value from them.

<html>
<head>
   <title> JavaScript - Math.max() method </title>
</head>
<body>
   <p id = "output"> </p>
   <script>
      let ans = Math.max(100, 10, -5, 89, 201, 300);
      document.getElementById("output").innerHTML = 
      "Math.max(100, 10, -5, 89, 201, 300) = " + ans + "<br>";
   </script>
</body>
</html>

Output

After executing the above program, it returns 300 as maximum value.

Math.max(100, 10, -5, 89, 201, 300) = 300

Example: Math cos() method

The Math.cos() method returns the cosine value of the number passed as an argument. The cosine value of 0 is 1, which you can see in the output of the example below.

<html>
<head>
   <title> JavaScript - Math.cos() method </title>
</head>
<body>
   <p id = "output"> </p>
   <script>
      let ans = Math.cos(0);
      document.getElementById("output").innerHTML = "Math.cos(0) = " + ans;
   </script>
</body>
</html>

Output

If we execute the above program, it returns "1" as result.

Math.cos(0) = 1
Advertisements