JavaScript absolute value: How Math.abs() handles common inputs

JavaScript Math.abs() result table for negative, positive, zero, and string inputs

JavaScript absolute value uses Math.abs() to return a number’s distance from zero. Negative values become positive, positive values stay unchanged, and zero remains zero. In other words, absolute value in JavaScript represents magnitude without a sign.

The syntax is Math.abs(value). The function accepts one argument and applies JavaScript’s numeric conversion before calculating the result, so its behavior includes more than number literals.

How does JavaScript absolute value work with Math.abs()?

For ordinary numeric input, Math.abs() returns the input’s nonnegative magnitude. It does not change the original variable; use the returned value when the normalized number is needed.

  • Math.abs(-12) returns 12.
  • Math.abs(12) returns 12.
  • Math.abs(0) returns 0.
  • Math.abs(-3.75) returns 3.75.

These results follow the mathematical definition: both -12 and 12 are 12 units from zero. The function removes the sign only when the value is negative; it does not round decimals or otherwise alter a finite number.

What does absolute value in JavaScript return for strings and null?

The JavaScript Math.abs function converts its argument to a number before applying absolute value. A string containing a valid numeric representation therefore produces a numeric result, while a string that cannot be converted produces NaN.

  • Math.abs(“-18”) returns 18.
  • Math.abs(“6.5”) returns 6.5.
  • Math.abs(” -9 “) returns 9; surrounding whitespace is ignored during conversion.
  • Math.abs(null) returns 0, because numeric conversion treats null as zero.
  • Math.abs(“18px”) returns NaN, because the complete string is not a valid number.
  • Math.abs(“hello”) returns NaN.

An empty string also converts to zero, so Math.abs(“”) returns 0. By contrast, Math.abs(undefined) returns NaN. NaN signals that the conversion did not produce a usable numeric result; it is not a positive or negative number.

How can Math.abs(a – b) find the distance between two numbers?

Use Math.abs(a – b) to calculate the absolute difference between two numbers. Subtraction happens first, and Math.abs() then removes the direction of that difference. This makes the result the same regardless of which number is larger.

For example, Math.abs(18 – 25) returns 7. Reversing the operands, as in Math.abs(25 – 18), also returns 7. The same pattern works with variables:

const distance = Math.abs(currentValue – targetValue);

If currentValue is 42 and targetValue is 50, distance is 8. This pattern is useful for comparing measurements, positions, scores, or other numeric values where only the size of the difference matters.

How does JavaScript Math.abs handle NaN, Infinity, and -0?

Special numeric values follow predictable rules:

  • Math.abs(NaN) returns NaN. An unknown numeric result remains unknown.
  • Math.abs(Infinity) returns Infinity.
  • Math.abs(-Infinity) returns Infinity.
  • Math.abs(-0) returns positive zero, displayed as 0.

JavaScript distinguishes positive and negative zero internally even though both usually display as 0. After Math.abs(-0), the sign is positive. The function also returns NaN when called without an argument: Math.abs() has no value to convert.