JavaScript Math abs() Method

The Javascript Math.abs() method is used to return the absolute value of a number. It takes a number as its parameter and returns its absolute value. Syntax:

Math.abs(value)

Parameters: This method accepts a single parameter as mentioned above and described below:

Returns: Absolute value of the number passed as a parameter. The below examples illustrate the Math abs( ) method in JavaScript:

Below is an example of the Math abs() method.

Example 1: This example shows the use of the Math.abs() method in javascript.




<!-- POSITIVE NUMBER EXAMPLE -->
<script type="text/javascript">
    console.log(Math.abs(2)+"<br>");
    console.log(Math.abs(2.56));       
</script>

Output:

2
2.56

Example 2: This example shows the return value of Math.abs() method when the parameter is a string value.






<!-- STRING EXAMPLE -->
<script type="text/javascript">
    console.log(Math.abs("Geeksforgeeks"));       
</script>

Output:

NaN

Example 3: This example shows the return value of Math.abs() method when the parameter is a arithmetic expression.




<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
<script type="text/javascript">
    console.log(Math.abs(7+9));       
</script>

Output:

16

Errors and Exceptions:

We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.

Supported Browsers:


Article Tags :