abs

Summary

The Math.abs(x) function returns the absolute value of a number "x".

Syntax

var abs = Math.abs(x);

Parameters

x
A number.

Description

Because abs is a static method of Math, you always use it as Math.abs().

Examples

Example: Behaviour of Math.abs()

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

Specifications

Specification Status Comment
ECMAScript 1st Edition. Implemented in JavaScript 1.0 Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
The definition of 'Math.abs' in that specification.
Standard  
ECMAScript 6 (ECMA-262)
The definition of 'Math.abs' in that specification.
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

See also

Document Tags and Contributors

Last updated by: Sevenspade,