Js to obtain the absolute value of a variable. js to obtain the absolute value of a variable.
This article describes how to obtain the absolute value of a variable in js. Share it with you for your reference. The specific analysis is as follows:
Absolute Value in js we directly use the abs function to find the value. Here we will sort out some methods for getting the absolute value of the variable in js, so that you can have a deeper understanding of the usage of the absolute value of js.
The absolute values in js are not very often used. Today I encountered this problem when I wrote a method, so I recorded it and learned it with everyone.
The default object in js-The Math object contains an abs function that is used to obtain the absolute value of a number, for example:
The Code is as follows: Math. abs (-1); // 1
Math. abs (-2); // 2
Of course, this function can also be used to obtain the absolute value of a variable, such:
The Code is as follows: var aaa =-3;
Var bbb = abs (aaa); // 3
Example:
The Code is as follows: <script language = "javascript">
Document. write ("the absolute value of 0 is:", Math. abs (0), "<br> ");
Document. write ("the absolute value of 1 is:", Math. abs (1), "<br> ");
Document. write ("the absolute value of-1 is:", Math. abs (-1), "<br> ");
// -->
</Script>
Another method:
We know that the absolute values in mathematics are decimal or integer, and so are they.
The Code is as follows: var aaa =-3.3;
Var bbb = abs (aaa); // 3.3
I hope this article will help you design javascript programs.