 JavaScript

# What is Math Object in JavaScript

JavaScript has an object called the Math Object which provides methods and properties that can be used to perform different mathematical operations in JavaScript code. Math is a built-in object like the Date object; but unlike the Date object it is not a constructor and has static properties; You do not need to create the Math object to use its properties and methods.

The Math object has many different objects and properties; some of which will be talked about in this post. First of all we will discuss the properties of the Math object

Note: The browser console is used for the demonstration of examples in this article.

## The properties of the Math Object in JavaScript

The Math object in JavaScript has eight different properties all of which return a constant. The .PI property is used to get the value of PI in JavaScript code:

var pi = Math.PI;
console.log(pi); The list of other seven properties is given below:

• E
• SQRT2
• SQRT1_2
• LN2
• LN10
• LOG2E
• LOG10E

Math.E : This property is used to get the Euler’s number:

var e = Math.E;
console.log(e); Math.SQRT2 : This property returns the value of the square root of 2:

var s = Math.SQRT2;
console.log(s); Math.SQRT1_2 : The Math.SQRT1_2 is used to get the value of the square root of half (½):

var s = Math.SQRT1_2;
console.log(s); Math.LN2 : When we need the value of the natural log of two in our code then we use the Math.LN2 property:

var l = Math.LN2;
console.log(l); Math.LN10 : We can get the value of the natural log of ten in JavaScript in the following way:

var l = Math.LN10;
console.log(l); Math.LOG2E : This method returns the base 2 logarithm of E:

var l = Math.LOG2E;
console.log(l); Math.LOG10E : Similar to the Math.LOG2E property this property also returns the value of the logarithm of E but of base 10:

var l = Math.LOG10E;
console.log(l); ## The methods of the Math Object in JavaScript

The Math object offers many methods in JavaScript; the most common of which will be discussed here:

## How to use the Math.round() method

The Math.round() method is used to round a number to its nearest integer:

var num = Math.round(1.4);
console.log(num); And if the digit after the decimal is 5 or greater than five then:

var num = Math.round(1.5);
console.log(num); Similarly, the Math.ceil() and the Math.floor() methods are used to round the number up to its nearest integer and down to its nearest integer respectively.  Two other methods Math.trunc() and Math.sign() were added in JavaScript in the ES6 version. Math.truc() function truncates the decimal part of an integer and returns the whole number whereas Math.sign() returns 1 or -1 to show whether the integer given to it was negative or positive. It returns 0 if you give it 0 as an argument:  ## How to use the Math.pow() and Math.sqrt() methods in JavaScript

The Math.pow() method takes two different numbers as arguments and returns the value of the first argument as the power of the second:

var num = Math.pow(2, 8);
console.log(num); The .sqrt() method takes a single argument and returns the value of its square root:

var num = Math.sqrt(256);
console.log(num); ## Conclusion

In JavaScript the Math Object provides different properties and methods to quickly perform a lot of mathematical operations. Some of these methods and properties were discussed in this article; these properties and methods are quite useful and commonly used in JavaScript programs.

The Math object also provides a ton of other handy methods as well such as the sin(), cos(), tan(), log(), and min(), max() etc. 