Open In App
Related Articles

JavaScript Math abs() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:

  • value: The number whose absolute value is to be found is passed as the parameter to this function.

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.

JavaScript

console.log(Math.abs(2));
console.log(Math.abs(2.56));

                    

Output
2
2.56

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

JavaScript

console.log(Math.abs("Geeksforgeeks"));

                    

Output
NaN

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

JavaScript

console.log(Math.abs(7+9));

                    

Output
16

Errors and Exceptions:

  • A non-numeric string passed as a parameter returns NaN.
  • An array with more than 1 integer passed as a parameter returns NaN.
  • An empty variable passed as a parameter returns NaN.
  • An empty string passed as a parameter returns 0.
  • An empty array passed as a parameter returns 0.

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:

  • Google Chrome 1 and above
  • Firefox 1 and above
  • Opera 3 and above
  • Safari 1 and above


Last Updated : 01 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads