We can remove the sign part of the data and display the absolute value by using abs function. Here is the syntax
Math.abs(x);
Sample code is here
<script language='JavaScript' type='text/JavaScript'>
<!--
document.write(Math.abs(-5)); // output is 5
document.write("<br>");
document.write(Math.abs(5.56)); // output is 5.56
document.write("<br>");
document.write(Math.abs(-2.5)); // output is 2.5
document.write("<br>");
document.write(Math.abs(-1.355)); // output is 1.355
document.write("<br>");
//-->
</script>
abs() math function can also be used with other calculations
document.write(Math.abs(43-64)); // output is 21
No comments:
Post a Comment