abs()
method
returns the absolute value of a number
abs() |
Yes | Yes | Yes | Yes | Yes |
Math.abs(x);
Parameter | Description |
---|---|
x | Required. A number |
It returns the absolute value of the specified number.
It returns NaN if the value is not a number, or 0 if the value is null.
The following code shows how to calculate the absolute value of num.
<!DOCTYPE html>
<html>
<body>
<script language="JavaScript">
function doMath() {<!-- w w w .j a v a 2 s . c o m-->
var inputNum = document.form1.input.value;
var result = Math.abs(inputNum);
document.form1.answer.value = result;
}
</script>
<form name=form1>
<br> Enter Number:
<input type="text" name="input" size=10>
<input type="button" value="Calculate" onClick='doMath()'> <br>
Answer: <input type="text" name="answer" size=10>
</form>
</body>
</html>