Search w3schools.com:

SHARE THIS PAGE

JavaScript abs() Method

Math Object Reference JavaScript Math Object

Example

Example

Return the absolute value of a number:

Math.abs(-7.25);

The result will be:

7.25

Try it yourself »

Definition and Usage

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


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The abs() method is supported in all major browsers.


Syntax

Math.abs(x)

Parameter Values

Parameter Description
x Required. A number

Return Value

Type Description
Number The absolute value of the specified number, or NaN if the value is not a number, or 0 if the value is null

Technical Details

JavaScript Version: 1.0


More Examples

Example

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 result of a,b,c,d, and e will be:

7.25
7.25
0
NaN
5

Try it yourself »


Math Object Reference JavaScript Math Object

W3Schools Certification

W3Schools' Online Certification

The perfect solution for professionals who need to balance work, family, and career building.

More than 10 000 certificates already issued!

Get Your Certificate »

The HTML Certificate documents your knowledge of HTML.

The HTML5 Certificate documents your knowledge of advanced HTML5.

The CSS Certificate documents your knowledge of advanced CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The jQuery Certificate documents your knowledge of jQuery.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]