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 list of other seven properties is given below:
Math.E : This property is used to get the Euler’s number:
Math.SQRT2 : This property returns the value of the square root of 2:
Math.SQRT1_2 : The Math.SQRT1_2 is used to get the value of the square root of half (½):
Math.LN2 : When we need the value of the natural log of two in our code then we use the Math.LN2 property:
Math.LOG2E : This method returns the base 2 logarithm of E:
Math.LOG10E : Similar to the Math.LOG2E property this property also returns the value of the logarithm of E but of base 10:
How to use the Math.round() method
The Math.round() method is used to round a number to its nearest integer:
And if the digit after the decimal is 5 or greater than five then:
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.
The Math.pow() method takes two different numbers as arguments and returns the value of the first argument as the power of the second:
The .sqrt() method takes a single argument and returns the value of its square root:
The Math object also provides a ton of other handy methods as well such as the sin(), cos(), tan(), log(), and min(), max() etc.