Computers are incredibly good at math. That’s why the invention of computers took place! Mathematicians and physicians invested hours calculating results from their formulas and data. Now, a computer can “compute” results for them in a snap of a finger. Moreover, in the earlier days, there used to be specific programming languages that majorly supported mathematical operations. FORTRAN is one of those examples. Still, with time and maturity of programming languages, mathematical calculations have become an integral part of every programming language, and JavaScript also is not an exception from the same. In this article, we will cover the details of the Math object in JavaScript, which helps the user to perform all kinds of mathematical operations.

• What is the Math object in JavaScript?
• Constants provided by Math object.
• Methods provided by Math Object.

## What is the Math object in JavaScript?

JavaScript’s Math object performs mathematical operations on numbers. Unlike other global objects, Math is not a constructor. In other words, all the properties and methods of Math are static, and one can call them by using Math as an object without creating it. Additionally, the Math object provides a set of properties and methods for mathematical constants and functions. If we are using an IDE/Editor such as “Visual Code,” we can check the properties and methods provided by the Math object by using the dot(.) operator with the “Math” object as shown below: As the above image shows, when we start scripting with the help of Visual Studio Code, and we type “Math.” the VS code IntelliSense provides a list of methods and variables available in Math Object.

Let’s discuss all these properties and methods provided by the Math object in detail in the below sections:

### Constants provided by Math Object

Constants are available in almost all programming languages. They are nothing but variables which can be assigned a value only once and can’t change again during the scope of the program. Assume you need to use a value of PI, i.e., 3.1416, in your programming in multiple places. If you start using the actual value, then there might be a chance where it can get missed or value change. In another case, if you need to change the value from 3.1416 to 3.142 to then, you need to change everywhere.

Another way to handle this is to create a variable and assign the value, use this variable in the program, but there is a chance where you can reassign it. There is a concept of constants, which helps to prevent this. Whereby, a programmer can assign value to a variable only once. Moreover, if a programmer tries to reassign a new value, the compiler will throw an error.

Now to ease it further, the Math object in JavaScript already contains certain variables such as PI, E, LN2, etc. and as the programmer can’t change the values of these variables. These are called constants provided by the Math object.

The JavaScript Math object provides following constants which solve various day to day mathematical problems:

• PI: Returns PI value that is 3.141592653589793
• SQRT2: Returns value of the square root of 2 that is 1.4142135623730951
• SQRT1_2: Returns value of the square root of ½ that is 0.7071067811865476
• LOG10E: Returns value of Log of e to the base 10 that is 0.4342944819032518
• E: Returns value of Euler’s number that is 2.718281828459045
• LN2: Returns value of Logarithmic value of 2 that is 0.6931471805599453
• LN10: Returns value of Logarithmic value of 10 that is 2.302585092994046
• LOG2E: Returns value of Log of e to the base 2 that is 1.4426950408889634

Let’s see the usage of all these constants with the help of following code snippet:

Save the file with the name mathConstants.html. After that, open it in any browser (Chrome, Firefox, or IE). It should show the output as: ### Methods provided by Math object

The Math object provides various methods also, which can perform mathematical calculations. Few of them are:

#### Abs()

This method will return the absolute value of a given number. Moreover, its syntax looks like below:

Syntax:

Let’s understand it further for various variable types with the help of following code snippet:

Save the file with name absoluteMethods.html. After that, open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### acos()

The acos() method will return the cosine value between 0 to value of PI (i.e., 3.141592653589793). The acos is the arccosine(in radians) of the given number. As we know, in mathematics, arccosine is an inverse of cosine.

Note: input can be a value between -1 to 1.

Additionally, its syntax looks like below:

Syntax:

Let’s understand the usage of acos() for various variable types with the help of following code snippet:

Save the file with name arccosineMethod.html. After that, open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### acosh()

This function calculates the hyperbolic arc cosine value for the provided input. Another name of the acosh() is the “inverse hyperbolic cosine” function. Moreover, its syntax looks like below:

Syntax:

Let’s understand the usage of acosh() for various variable types with the help of following code snippet:

Save the file with name hyperbolicArccosineMethod.html. After that, open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### asin()

The asin() method will return the sine value between -PI/2 to  PI/2. The asin is the arcsine(in radians) of the given number. As we know, in mathematics, arcsine is an inverse of sine.

Note: input can be a value between -1 to 1.

Additionally, its syntax looks like below:

Syntax:

Let’s understand the usage of asin() for various variable types with the help of following code snippet:

Save the file with name arcsineMethod.html. After that, open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### asinh()

This function calculates the hyperbolic arcsine value for the provided input. Another name for asinh() is the “inverse hyperbolic sine” function. Moreover, its syntax looks like below:

Syntax:

Let’s understand the usage of asinh() for various variable types with the help of following code snippet:

Save the file with name hyperbolicArcsineMethod.html. After that, open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Atan()

The atan() method returns the tangent value between -PI/2 to  PI/2. The atan() is nothing but arctangent(in radians) of number. As we know in mathematics, arctangent is an inverse of tangent. Additionally, its syntax looks like below:

Syntax:

Let’s understand the usage of atan() for various variable types with the help of following code snippet:

Save the file with name arctanMethod.html. After that, open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Atanh()

The atanh() function returns the hyperbolic arctan value for the provided input. Another name for atanh() is the “inverse hyperbolic tangent” function. Additionally, its syntax looks like below:

Syntax:

Let’s understand the usage of atanh() for various variable types with the help of following code snippet:

Save the file with name hyperbolicArctanMethod.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Cbrt:

This function returns the cube root of the provided input. Its syntax looks like below:

Syntax:

Let’s understand the usage of cbrt() for various variable types with the help of following code snippet:

Save the file with name cuberoot.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### sqrt()

This function returns the square root of the provided input. Its syntax looks like below:

Syntax:

Let’s understand the usage of sqrt() for various variable types with the help of following code snippet:

Save the file with name squareroot.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### ceil()

This function returns the ceiling value of the input. E.g., if the input is 5.3, then the ceiling value will 6. Its syntax looks like below:

Syntax:

Let’s understand the usage of ceil() for various variable types with the help of following code snippet:

Save the file with name ceil.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Floor:

This function returns the floor value of the provided input. E.g., the floor value of 5.8 will be 5. Its syntax looks like below:

Syntax:

Let’s understand the usage of floor() for various variable types with the help of following code snippet:

Save the file with name floor.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Exp

This function returns the exponential value of the given input. Its syntax looks like below:

Syntax:

Let’s understand the usage of exp() for various variable types with the help of following code snippet:

Save the file with name exp.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Round:

This function returns the rounded value of decimals. Its syntax looks like below:

Syntax:

Let’s understand the usage of round() for various variable types with the help of following code snippet:

Save the file with name round.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Max

This function returns the maximum value in the given set of values. Its syntax looks like below:

Syntax:

Let’s understand the usage of max() for various variable types with the help of following code snippet:

Save the file with name max.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Min

This function returns the minimum value in the given set of values. Its syntax looks like below:

Syntax:

Let’s understand the usage of min() for various variable types with the help of following code snippet:

Save the file with name min.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Pow

This function returns the power value of the given input. Its syntax looks like below:

Syntax:

Let’s understand the usage of pow() for various variable types with the help of following code snippet:

Save the file with name pow.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: #### Random

This function returns a random value between 0 to 1. Its syntax looks like below:

Syntax:

Let’s understand the usage of random() for various variable types with the help of following code snippet:

Save the file with name random.html and open it in any browser (Chrome, Firefox, or IE). It should show the output as: ## Key Takeaways

• The “Math” object provides various properties and methods which can perform mathematical operations in JavaScript.
• Moreover, you can use mathematical constants such as PI, Log values, etc. using the properties provided by Math object such as  Math.PI, Math.Log2E.
• In addition to the above, you can invoke the methods provided by Math object such as abs(), asin(), ceil(), etc. to perform various mathematical operations on multiple variables.

To conclude, let’s now move to the next article, “Deep dive into objects,” to understand the concepts of Objects in JavaScript in more detail.