Javascript Math Reference
Javascript Math.abs
The Math.abs(x) returns the absolute value of the x. Take a look at the following example
<html> <body> <script type="text/javascript"> <!-- /* ******************************************************** Example - Usage of Math.pow ******************************************************** */ var x =-4; var y = 3; var z1 = Math.abs(x); var z2 = Math.abs(y); document.write ("Math.abs(-4) is : " + z1 +"<br />" ); document.write ("Math.abs(3) is : " + z2); //--> </script> </body> </html> |
Math.abs takes a single argument and if the argument is -ve, it returns its absolute positive value. You may like to try this program online here . Make changes in the program and see its results.
As a practical example, try to print a table showing financial results of a company with three rows and two columns. The first row will have the total sale. The second row contains the expenses. The third row contains the profit / (loss). if it is loss, its absolute value us printed within bracket. As an additional exercise, try printing that value in red.
Here is one way, the above exercise has been implemented Real Life Math.abs example . Make sure you see this example once you have given your fair try.