Math.abs()

abs(x)
Returns: Number · Added in vES1 · Updated March 13, 2026 · Math
javascript math abs absolute

The Math.abs() function returns the absolute value of a number. In other words, it returns the positive version of any numeric value.

Syntax

Math.abs(x)

Parameters

ParameterTypeDescription
xNumberA number whose absolute value you want to compute.

Return Value

  • The absolute value of the given number.
  • Returns NaN if the argument is NaN or non-numeric.
  • Returns Infinity if the argument is Infinity or -Infinity.

Examples

Basic Usage

Math.abs(5);
// 5

Math.abs(-5);
// 5

Math.abs(0);
// 0

With Different Numeric Types

Math.abs(-3.14);
// 3.14

Math.abs(Infinity);
// Infinity

Math.abs(-Infinity);
// Infinity

With Non-Numeric Values

Math.abs("hello");
// NaN

Math.abs(NaN);
// NaN

Practical Example: Distance Calculation

function distance(x1, y1, x2, y2) {
  return Math.sqrt(
    Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)
  );
}

distance(0, 0, 3, 4);
// 5

distance(1, 2, 1, 2);
// 0

Practical Example: Temperature Range

function temperatureRange(low, high) {
  return Math.abs(high - low);
}

temperatureRange(20, 30);
// 10

temperatureRange(30, 20);
// 10

See Also