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);

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 2: Math.abs() with Numeric strings

In the example below, we use the JavaScript 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

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 Absolute value without math

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