Signup/Sign In
LAST UPDATED: MAY 9, 2020

JavaScript Math Object

In JavaScript, Math is a built-in object which includes properties and methods for mathematical operations. We can use the Math object to perform simple and complex arithmetic operations.

Note: Math works with the JavaScript Number type only.

In Math object, All the properties and methods are static. So, we don't need to create its object to use its property or method. Also, even if we want, we cannot create an object as Math is not a constructor function.

Using JavaScript Math

Suppose, we want to get the value of PI for geometric calculation or the square root of a number in our program then we can use Math object. Let's see how:

let pi = Math.PI;
document.write(pi + "<br/>");

let sqrt = Math.sqrt(9);
document.write(sqrt);


3.141592653589793
3

JavaScript provides a rich set of Math properties that can be used to get predefined constant values.

JavaScript Math Properties

Following are the properties provided by the JavaScript Math object:

Property Description
E holds Euler number whose value is 2.718(approx)
LN2 holds a natural logarithm of 2 whose value is 0.693(approx).
LN10 holds a natural logarithm of 10, whose value is 2.302(approx).
LOG2E holds the base-2 logarithm of E have value1.442(approx)
LOG10E holds the base-10 logarithm of E having value 0.434(approx)
PI holds the numerical value of PI, whose approx value is 3.142
SQRT1_2 holds the square root of 1/2, whose approx value is 0.707
SQRT2 holds the square root of 2, having approx value 1.414

Let's take an example to see how we can use these properties.

var e  = Math.E
document.write(e +"<br>")

var ln2  = Math.LN2
document.write(ln2 +"<br>")

var ln10  = Math.LN10
document.write(ln10 +"<br>")

var sq = Math.SQRT1_2
document.write(sq +"<br>")


2.718281828459045
0.6931471805599453
2.302585092994046
0.7071067811865476

Find Minimum and Maximum Number

Suppose, we want to find min and max numeric value from the random number of list then it can be very easy by using Math object. See the below example.

// Find minimum number
var min = Math.min(10,25,47,23,18,8,21,30)
document.write(min +"<br>")

// Find maximum number
var max = Math.max(10,25,47,23,18,8,21,30)
document.write(max +"<br>")


8
47

JavaScript Math Methods

JavaScript Math Object provides a rich set of methods that are used to make math calculation easy and helps to reduce effort and time in math-oriented programming. Here we have listed the most commonly used Math object methods:

Method Description
abs(x) returns the absolute value of x
ceil(x) rounds up x to a nearest biggest integer
cos(x) returns the cosine value of x
exp(x) returns the value of the exponent.
random() returns a random number between 0 and 1
tan(x) returns the tangent value of x
sqrt(x) returns the square root of x
sin(x) returns the sine value of x.
floor(x) rounds up x to the nearest smallest integer.
max(x,y,z,......n) returns the highest number from the lsit.
min(x,y,z,.......n) returns the lowest number from the list.
pow(x,y) returns x to the power of y
cos(x) returns the cosine value of x
log(x) returns the logarithmic value of x

Let's see an example in which we are using Math methods to understand the return value of those methods:

So to use Math object properties or methods, we do not have to create an object using the new keyword, and we can directly use the properties and methods in our code.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight