JavaScript abs

The JavaScript abs function is a math function that returns the absolute value (positive) of the given number or specified expression. In short, it removes the negative symbol (if any) of the given number and returns the positive value.

JavaScript abs() syntax

The syntax of the abs function is shown below.

Math.abs(number);

Parameters: This method accepts a number whose absolute value has to be found.

Return Value: The abs() function returns the absolute value (positive) of the given number.

  • When we pass a positive or negative number argument, the abs function will return the absolute value of it. If we pass a positive number (n), it returns the same number (n). When we pass a negative number (-n), it returns the positive version of it (n).
  • If it is not a number or non-numeric string, the abs function returns NaN.
  • If the parameter value is null, an empty string, or an empty array, the abs function returns zero.

JavaScript abs Function Example

In this example, we use the Math.abs() function to find the absolute values of different data types and display the output. First, we used the absolute function directly on both positive and negative integers. Both of them will return the same output.

Math.abs(20);
Math.abs(-20);

Next, we used the JavaScript abs Function on the Positive and negative Decimal values. If you observe the screenshot, it works correctly with decimal values.

Math.abs(10.45);
Math.abs(-6.45);

On the next line, we tried the JavaScript Function on the string value. As it cannot be converted to a numeric value, the abs() function returns NaN.

Math.abs("JavaScript");

Next, we tried this Math function on the Null value, and the abs() method returned zero as output. You can try an empty string or an array; it returns 0.

Math.abs(null);

In the statement below, we used the abs Function directly on multiple values inside an expression. It means, abs(2 + 55 – 77) => abs(-20) = 20.

Math.abs(2 + 55 - 77);

TIP: Use the Math.sign() function inside the if-else statement to get the sign before finding the absolute value.

Example

<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptABSFunction</title>
</head>
<body>
  <h1> JavaScriptABSFunction</h1>
  <p id = "Pos"></p>
  <p id = "Neg"></p>
  <p id = "Dec"></p>
  <p id = "Neg_Dec"></p>
  <p id = "Str"></p>
  <p id = "Exp"></p>
  <p id = "Null"></p>
  <p id = "Multi"></p>
  <script>
    document.getElementById("Pos").innerHTML = Math.abs(20);
    document.getElementById("Neg").innerHTML = Math.abs(-20);
    document.getElementById("Dec").innerHTML = Math.abs(10.45);
    document.getElementById("Neg_Dec").innerHTML = Math.abs(-6.45);
    document.getElementById("Str").innerHTML = Math.abs("JavaScript");
    document.getElementById("Null").innerHTML = Math.abs(null);
    document.getElementById("Multi").innerHTML = Math.abs(2 + 55 - 77);
  </script>
</body>
</html>
JavaScript abs function example

Example 3: In this example, we use the Math.abs() function on positive and negative 0.

console.log(Math.abs(0));
console.log(Math.abs(-0));
0
0

Example 4: Using Math.abs() on Infinity values

If we pass positive or negative infinity values to the abs() function, it returns Infinity as the absolute value.

console.log(Math.abs(Infinity));
console.log(Math.abs(-Infinity));
Infinity
Infinity

JavaScript math abs string

In the example below, we use the abs() function to find the absolute value of the numeric string. If it can be implicitly converted to a numeric value (integer or float), Math.abs returns the absolute value.

let s= "-100.89"
console.log(Math.abs(s));
100.89

JavaScript math abs array

If you want to find the absolute positive value of every item in an array, we can use the map() function and the Math.abs() function. Here, the map() implements the Math.abs() function on each array item and returns a new array with the absolute values.

const a = [10, -20, -30, 40, -50, 60];
const pa = a.map(Math.abs);
console.log(pa);
(6) [10, 20, 30, 40, 50, 60]

JavaScript Absolute value without math abs

We can use the conditional expression to check whether the given value is less than 0 or not. If it is negative, convert it to a positive value.

const a = n => (n < 0 ? -n : n);

console.log(a(-10));
console.log(a(-50.98));
console.log(a(10));
10
50.98
10

Related Math functions