JavaScript Math

Welcome to the JavaScript tutorials, we have already covered JavaScript arrays object, and JavaScript Date object. Today we will discuss about ‘JavaScript Math’ object. Its name suggests that it will be used for Mathematical problems. Yes, it is used to perform mathematical tasks.

The JavaScript math object is a predefined for mathematical function or methods. In simple term, You cannot create its object. You can call any function or property by using math as an object.

JavaScript Math – Syntax

We already mentioned, that we need not to create its object like other JavaScript objects. The simplest form of syntax is:

Math.property

Or

Math.method

For example, if you want to return the absolute value of the number, then you can use abs () method, like:

Var sharp = Math.abs();

JavaScript math – Properties

Now we will see the properties that you can use in your project. Probably you will get the complete list in our JavaScript reference.

SQRT2 – It will return the square root of 2. It will give you the approx value.

SQRT1_2 – it will return the square root of ½.

PI – It will return the value of pi (22/7).

LN2 – It will return the natural logarithm of 2.

LN10 – It will return the natural logarithm of 10.

LOG10E – It will show the base 10 logarithm of E.

Example – JavaScript math properties

Now let’s try to understand the use of properties with the simple to understand example. We are using ‘SQRT2’ property to understand the use of properties. As we know that ‘SQRT2’ will return the square root of the 2. Let’s see the output in the below code.

Yes, the output is the square root of the 2. It is an approx value of the root 2. We just used the predefined JavaScript math object to call the property ‘ SQRT2’.

JavaScript math – Methods

In this section, we will list some of the frequently used methods by the developer.

abs () – The ‘abs’ is short for Absolute, and it will return the absolute value of a number.

acos () – Returns the arccosine in radians of the number.

asin () – We can use it to get arcsine in radians of the number.

atan () – We can use it to get the arctangent in radians of the number.

ceil () – We can use it to get the smallest integer (either greater than or equal to the number).

cos () – We can use it to get the cosine of the number in radians.

exp () – We cause use it to get the value of En, here E stands for ‘Euler’ and n stands for the ‘argument’.

floor ()- We can use it to get the largest integer (either greater than or equal to the number).

pow () – W can use it, to get the base exponent of the number.

random () – We can use it to get the random numbers between the 0 and 1.

sqrt() – We can use it to get the square root of a number.

tan() – We can use it to find the tangent of the number.

Note: We have not listed all the methods of JavaScript math. You can refer JavaScript Reference.

Example – JavaScript math methods

Let’s see the use of one of the method to understand the use of the rest of the functions. In the below example, we are using sqrt() method to show you the use of method without creating the JavaScript math object.

First perfect square : 7
It is not a perfect square : 6.992853494818835

We have called the sqrt() method by using the predefined object ‘ math’. Like the above example, you can try the methods. It is not at all hard to implement methods.

Tips to Remember

You need not to create its object first, because it has no constructor.

The properties and methods of JavaScript math are static.

Conclusion

We have just seen the beauty of the JavaScript math object. Beside the above example, you should practice more. Because JavaScript is a beautiful and easy to learn language. Just don’t forget the practice, do it to become perfect.

What next? Next we will see JavaScript Regular Expression.

If you liked our content on JavaScript math, then do share it with friends. You can like us on Facebook or can tweet with us as well. If you have anything to share or a query, then do write to us. Because at SharpLesson, readers are free to share their views