Tutorials

# JavaScript Math Functions That Every Developer Should Know

The JavaScript Math object is a built-in object with attributes and methods for mathematical constants and functions and methods for executing mathematical operations. It’s not a constructor or a function object. Because Math’s properties and methods are static, we can use it as an object without creating it. In this article, we will be taking a look at some of the top JavaScript Math functions.

## Math Functions in JavaScript

Methods and attributes make up Math functions. The following is a list of the Math object’s methods:

#### round()

This method converts the given number’s value to a rounded integer. Math.round(x), where x is a number, is a way to express it.

#### pow()

It gives a to the power of the b value.

#### sqrt()

It returns the square root of a number. It’s possible to write it as:

Math.sqrt(a), where a is a number.

#### abs()

It returns a number’s absolute, i.e., positive, value. It’s possible to write it as:

Math.abs(a); where a is a number.

Example:

```<html>
<body>
<p id=" demo_abs "></p>
<script>
document.getElementById("abs_demo").innerHTML = Math.abs(-6.7);
</script>
</body>
</html>```

#### ceil()

It returns a smaller number that is either greater than or equal to the specified integer. It’s possible to write it as:

Math.ceil(a); where a is a number.

Example:

```<html>
<body>
<p id="demo_ceil "></p>
<script>
document.getElementById("ceil_demo").innerHTML = Math.ceil(6.8);
</script>
</body>
</html>```

#### floor()

It returns a larger number that is either less than or equal to the input integer. It’s possible to write it as:

Math.floor(a); where a is a number.

Example:

```<html>
<body>
<p id="demo_floor "></p>
<script>
document.getElementById("floor_demo").innerHTML = Math.floor(6.7);
</script>
</body>
</html>```

#### sin()

It calculates the sine of a given number. It’s possible to write it as:

Math.sin(a); where a is a number.

Example:

```<html>
<body>
<script type = "text/javascript">
var value = Math.sin( 3.5 );
document.write("First-Value : " + value );
var value = Math.sin( 80 );
document.write("<br />Second-Value : " + value );
var value = Math.sin( Math.PI/2 );
document.write("<br />Third-Value : " + value );
</script>
</body>
</html>```

#### cos()

It calculates the cosine of a given number. It’s possible to write it as:

Math.cos(a); where a is a number.

Example:

```<html>
<body>
<script type = "text/javascript">
var value = Math.cos(80);
document.write("First-Value : " + value );
var value = Math.cos(-1);
document.write("<br />Second-Value : " + value );
var value = Math.cos(2*Math.PI);
document.write("<br />Third-Value : " + value );
</script>
</body>
</html>```

#### min() and Math.max()

The min() method shows the value of the given arguments that is the smallest. Math.min(val1, val2………valn); where val1, val2………valn are numbers.

The maximum value of the provided arguments is displayed using the max() function. Math.max(val1, val2………valn); where val1, val2………valn are numbers.

Example:

```<html>
<body>
Minimum Value: <p id="min-demo"></p>
Maximum Value: <p id="max-demo"></p>
<script>
document.getElementById("min-demo").innerHTML =
Math.min(70, 57, 25, 15, 98, 14);
document.getElementById("max-demo").innerHTML =
Math.max(40, 80, 55, 35, 68, 24);
</script>
</body>
</html>```

#### random()

It generates a number between 0 and 1 at random. It’s possible to write it as:

Math.random();

Example:

```<html>
<body>
<script type = "text/javascript">
var value = Math.random( );
document.write("First-Value : " + value );
var value = Math.random( );
document.write("<br />Second-Value : " + value );
var value = Math.random( );
document.write("<br />Third-Value : " + value );
</script>
</body>
</html>```

#### acos()

It returns an integer’s arccosine. It’s possible to write it as:

Math.acos(a); where a is a number.

Example:

```<html>
<body>
<script type = "text/javascript">
var value1 = Math.acos(-1);
document.write("First-Value : " + value1 );
var value2 = Math.acos(null);
document.write("<br />Second-Value : " + value2 );
var value3 = Math.acos(30);
document.write("<br />Third-Value : " + value3 );
var value4 = Math.acos("string");
document.write("<br />Fourth-Value : " + value4 );
</script>
</body>
</html>```

#### asin()

It returns an integer’s arcsine. It’s possible to write it as:

Math.asin(a); where a is a number.

Example:

```<html>
<body>
<script type = "text/javascript">
var value1 = Math.asin(-1);
document.write("First-Value : " + value1 );
var value2 = Math.asin(null);
document.write("<br />Second-Value : " + value2 );
var value3 = Math.asin(30);
document.write("<br />Third-Value : " + value3 );
var value4 = Math.asin("string");
document.write("<br />Fourth-Value : " + value4 );
</script>
</body>
</html>```

### List of Properties That Can be Utilized With a Math Object

Now we will look at some properties that can be used with a Math Object:

#### E stands for Euler’s number

Example:

```<html>
<body>
<script type = "text/javascript">
var demo_value = Math.E
document.write("Value is :" + demo_value);
</script>
</body>
</html>```

#### PI calculates the PI value

Example:

```<html>
<body>
<script type = "text/javascript">
var demo_value = Math.PI
document.write("Value is :" + demo_value);
</script>
</body>
</html>```

#### SQRT2 stands for square root of two

Example:

```<html>
<body>
<script type = "text/javascript">
var demo_value = Math.SQRT2
document.write("Value is :" + demo_value);
</script>
</body>
</html>```

#### The square root of 1/2 is specified by SQRT1 2

Example:

```<html>
<body>
<script type = "text/javascript">
var demo_value = Math.SQRT1_2
document.write("Value is :" + demo_value);
</script>
</body>
</html>```

#### The natural logarithm of 2 is denoted by the symbol LN2

Example:

```<html>
<body>
<script type = "text/javascript">
var demo_value = Math.LN2
document.write("Value is :" + demo_value);
</script>
</body>
</html>```

#### The natural logarithm of ten is denoted by the symbol LN10

Example:

```<html>
<body>
<script type = "text/javascript">
var demo_value = Math.LN10
document.write("Value is :" + demo_value);
</script>
</body>
</html>```

#### The BASE 2 logarithm of E is specified by LOG2E

Example:

```<html>
<body>
<script type = "text/javascript">
var demo_value = Math.LOG2E
document.write("Value is :" + demo_value);
</script>
</body>
</html>```

#### The BASE 10 logarithm of E is specified by LOG10E

Example:

```<html>
<body>
<script type = "text/javascript">
var demo_value = Math.LOG10E
document.write("Value is :" + demo_value);
</script>
</body>
</html>```

As previously stated, the Math object cannot be used as the constructor. It has built-in attributes and techniques for carrying out mathematical operations on numbers. Instead of utilizing the prototype, you can extend the Math object directly to make use of it.

Check out some additional highly useful JavaScript libraries here. 