The abs() method returns the absolute value of a number.
The abs() method returns the absolute value of a number.
Math.abs(x)
Parameter | Require | Description |
---|---|---|
x | Required. | A number |
A Number, representing the absolute value of the specified number,
NaN if the value is not a number
0 if the value is null
Return the absolute value of a number:
//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));