JavaScript abs() Method

Definition and usage

abs() The method returns the absolute value of a number.

Instance

Example 1

Return the absolute value of a number:

Math.abs(-7.25);

Try it yourself

Example 2

Return the absolute value of different numbers:

var a = Math.abs(7.25);
var b = Math.abs(-7.25);
var c = Math.abs(null);
var d = Math.abs("Hello");
var e = Math.abs(2+3);

The results of a, b, c, d, and e will be:

7.25
7.25
0
NaN
5

Try it yourself

Syntax

Math.abs(x)

Parameter value

Parameter Description
x Required. Must be a number.

Technical details

Return value: A number, representing the absolute value of the specified number. If the value is not a number, it returns NaN. If the value is empty, it returns 0.
JavaScript version: ECMAScript 1

Browser Support

Math.abs() Is an ES1 feature (JavaScript 1999). All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support

Related Pages

Tutorial:JavaScript Math