Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Math.abs

Returns the absolute value of a number — its value without the sign.

Available since: ES1

Syntax

Math.abs(x)

Parameters

ParameterTypeDescription
xnumberAny number.

Return value

numberThe number without the minus sign.

Examples

The absolute value of a positive and a negative number:

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

Output:

5
5

The difference between two numbers in any order:

const a = 3;
const b = 10;
 
console.log(Math.abs(a - b));

Output:

7

Notes

A numeric string is converted automatically: Math.abs('-2') returns 2, and a non-numeric string gives NaN.

See also

  • Math.sign — Returns the sign of a number: 1, -1 or 0.
  • Math.max — Returns the largest of the given numbers.

Related recipes

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.