Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Math:abs

From MDC


Contents

[edit] Summary

Returns the absolute value of a number.

Method of Math

Static

Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

[edit] Syntax

var abs = Math.abs(x);

[edit] Parameters

x 
A number.

[edit] Description

abs is a static method of Math, so you always use it as Math.abs(), rather than as a method of a Math object you created.

[edit] Examples

[edit] Example: Math.abs behavior

Passing a non-numeric string or undefined/empty variable returns NaN. Passing null returns 0.

Math.abs('-1');     // 1
Math.abs(-2);       // 2
Math.abs(null);     // 0
Math.abs("string"); // NaN
Math.abs();         // NaN