Math.abs Examples
The general syntax for this method is:
Math.abs(number)
This method returns the positive value of a number, which can be printed or
stored in a variable. abs
works on both integer and decimal values.
Numerical strings can also be evaluated, but should be avoided as a best practice.
Example
|
|
Console Output
3
4.44
3.33
8
Math.abs
also works on arrays, but to make the process work, we must
combine it with the map array method. The syntax for this
is:
arrayName.map(Math.abs)
Note that Math.abs
takes no argument when applied to an array.
Example
|
|
Console Output
[2, 3, 4.44, 8.88]