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 Language Specification 5.1th Edition (ECMA-262) Standard  
ECMAScript Language Specification 6th Edition (ECMA-262) 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

Contributors to this page: Sheppy
Last updated by: Sheppy,