JavaScript abs() metoden

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 that represents 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

Webbläsarstöd

Math.abs() Är ES1-funktioner (JavaScript 1999). Alla webbläsare stöder det fullt ut:

Chrome IE Edge Firefox Safari Opera
Stöd Stöd Stöd Stöd Stöd Stöd

Relaterade sidor

Lär dig:JavaScript matematik