This paper illustrates the method of obtaining the absolute value of variables in JS. Share to everyone for your reference. The specific analysis is as follows:
Absolute value in JS we directly use the ABS function to seek, here to sort out some of the absolute value of the variable in JS, so that we have a deeper understanding of the use of JS absolute value
JS in the absolute value is not commonly used to, today in the writing of a method encountered, and then recorded, and we learn together.
The default object--math object in JS has an ABS function, which is designed to get the absolute value of the number, such as:
Copy Code code as follows:
Math.Abs (-1); 1
Math.Abs (-2); 2
Of course, this function can also be used to get the absolute value of a variable, such as:
Copy Code code as follows:
var aaa=-3;
var bbb=abs (AAA); 3
Example:
Copy Code code as follows:
<script language= "JavaScript" >
document.write ("0 of the absolute value is:", Math.Abs (0), "<br>");
document.write ("1 of the absolute value is:", Math.Abs (1), "<br>");
document.write ("1 of the Absolute value:", Math.Abs ( -1), "<br>");
-->
</script>
Another method:
We know that the absolute value in mathematics is either Decimal or integer, and this is certainly the case.
Copy Code code as follows:
var aaa=-3.3;
var bbb=abs (AAA); 3.3
I hope this article will help you with your JavaScript programming.