You can calculate absolute value of a any number with the help of math.abs() method in JavaScript
You can use this methods as :
Math.abs(number)
where number is number for which the absolute value is needed.
see this :
<script type="text/javascript"> document.write(Math.abs(23.7) + "<br />"); document.write(Math.abs(-23.7) + "<br />"); document.write(Math.abs(5-10)); document.write(Math.abs(null) + "<br />"); </script> The output will be: 23.7 23.7 5 0
Example:
The following example create a form with two textbox and one button, when user click on button ‘txtResult’ shows absolute value according to enter number in ‘txtNumber’ number.
<html> <body> <script language="JavaScript"> <!-- function GetAbsolute() { var number=document.form1.txtNumber.value; var absValue = Math.abs(number); document.form1.txtResult.value = absValue } --> </script> <form name=form1> Enter Number: <input type="text" name="txtNumber" size=10> <br> Absolute Value: <input type="text" name="txtResult" size=10> <br> <input type="button" value="Find absolute" onClick='GetAbsolute()'> </form> </body> </html>