Js to obtain the absolute value of a variable. js to obtain the absolute value of a variable.

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.