The
Math.abs()
method returns the absolute value of a number.
Description
The
abs()
static method of the
Math
object returns the absolute value of a number. The absolute value of a number is its non-negative value, regardless of whether it’s positive or negative.
If the argument passed to
Math.abs()
isn’t a number, the result will be
NaN
.
Parameters
Number
(optional)— A numeric value whose absolute value will be returned.
Return Type
A
Number
or
Nan
.
Examples
console.log(Math.abs(-3)); // Output: 3
console.log(Math.abs(2.5)); // Output: 2.5
With Non-numeric Values
console.log(Math.abs("10")); // Output: 10
console.log(Math.abs("test")); // Output: NaN
See Also
Math.sign()
— Returns the sign of a number, indicating whether it’s negative, positive, or zero.
Full Developer Reference
Attribution
- "Original Content" by Mozilla Contributors, licensed under CC-BY-SA 2.5 | Modified from the Original