Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Math.abs

Math.abs() returns the absolute value of a number. It converts its argument to a Number and returns a non-negative result, or NaN when conversion is not possible.

How it works

Math.abs() converts its argument to a Number and returns its magnitude without a negative sign. Negative values, including negative zero, are sign-reversed; other numeric values are returned unchanged, while values that cannot be converted produce NaN.

Syntax

Math.abs(x)

Parameters

NameTypeDescription
xnumberThe value whose absolute magnitude should be returned; it is converted to a number.

Returns

The absolute value of x; the result is a positive number or 0, or NaN when conversion fails.

Key points

  • It removes a negative sign from a numeric value.
  • Negative inputs are transformed by taking their opposite.
  • Zero and positive inputs keep their value.
  • The returned number cannot be negative.
  • Negative infinity becomes positive infinity.
  • Omitting the argument yields NaN because the missing value cannot become a useful number.

Common mistakes

  • The argument is converted to a number first, so strings and other coercible values may produce numeric results.
  • Objects or other inputs that cannot be converted to a number result in NaN.
  • Call it through the built-in Math namespace; do not treat it as an instance method.

Example: Finding an absolute difference

const difference = (a, b) => Math.abs(a - b);

console.log(difference(3, 5)); // 2
console.log(difference(5, 3)); // 2

Computes the non-negative distance between two numbers regardless of their order.

When to use it

Absolute differences

Compute distance or difference without caring which operand is larger.

Non-negative magnitudes

Normalize a signed measurement into a magnitude.

Tolerance comparisons

Check whether a numeric result is within a tolerance.

Interview questions

QWhat does Math.abs() return?

Math.abs(x) returns the absolute value of x, so the result is non-negative. A negative input is changed to its opposite, while a non-negative input is returned unchanged.

Follow-up: What happens for -0? It returns positive zero, because negative zero is explicitly converted to its opposite.

QHow does Math.abs() handle non-number arguments?

The function first converts its argument to a number. For example, a numeric string can become a number, while an input that cannot be converted produces NaN.

Follow-up: What does Math.abs([2]) return? The array is converted to the string "2", then to the number 2, so the result is 2.

QWhy is the call written Math.abs(x)?

Math.abs() is a static method of the Math namespace, and Math is not a constructor. Therefore, call it as Math.abs(value), rather than through a Math instance.

Follow-up: Can Math.abs() process BigInt values? No; Math functions operate on Number values, not BigInt values.

  • Math.hypot()
  • Math
  • Number

Source: Math.Abs. Checked 2026-10-15.