Javascript - Math abs() Method

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

Description

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

Syntax

Math.abs(x)

Parameter Values

Parameter Require Description
x Required. A number

Return

A Number, representing the absolute value of the specified number,

NaN if the value is not a number

0 if the value is null

Example

Return the absolute value of a number:

Demo

//the absolute value of -7.25
console.log(Math.abs(-7.25));/*from w w w  . j  ava2  s  . c  o m*/

//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);

var x = a + "\n" + b + "\n" + c + "\n" + d + "\n" + e;
console.log(x);
console.log(Math.abs(undefined));

Result