JavaScript abs() metode

Definition and usage

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

Example

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

Browserstøtte

Math.abs() Er ES1 egenskab (JavaScript 1999). Alle browsere understøtter det fuldt ud:

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

Relaterede sider

Tilførsel:JavaScript matematik