How to use the Math.abs() function get the absolute value of a number in JavaScript

1 Answer

0 votes
document.write("Math.abs(10) = " + Math.abs(10) + "<br />");
document.write("Math.abs(-2) = " + Math.abs(-2) + "<br />");
document.write("Math.abs('-3') = " + Math.abs('-3') + "<br />");
document.write("Math.abs([]) = " + Math.abs([]) + "<br />");
document.write("Math.abs([1,2,3]) = " + Math.abs([1,2,3]) + "<br />");
document.write("Math.abs('') = " + Math.abs('') + "<br />");
document.write("Math.abs(null) = " + Math.abs(null) + "<br />");
document.write("Math.abs([3]) = " + Math.abs([3]) + "<br />");
document.write("Math.abs({}) = " + Math.abs({}) + "<br />");
document.write("Math.abs('abc') = " + Math.abs('abc') + "<br />");
document.write("Math.abs() = " + Math.abs() + "<br />");
document.write("Math.abs(-3.14) = " + Math.abs(-3.14) + "<br />");

 
/*
run

Math.abs(10) = 10
Math.abs(-2) = 2
Math.abs('-3') = 3
Math.abs([]) = 0
Math.abs([1,2,3]) = NaN
Math.abs('') = 0
Math.abs(null) = 0
Math.abs([3]) = 3
Math.abs({}) = NaN
Math.abs('abc') = NaN
Math.abs() = NaN
Math.abs(-3.14) = 3.14
 
*/

 



answered Jul 31, 2016 by avibootz
...