Home > Web Front-end > JS Tutorial > body text

How to get the absolute value of a variable in js_javascript skills

WBOY
Release: 2016-05-16 16:22:34
Original
1094 people have browsed it

The example in this article describes the method of obtaining the absolute value of variables in js. Share it with everyone for your reference. The specific analysis is as follows:

In js, we directly use the abs function to find the absolute value. Here are some methods for obtaining the absolute value of variables in js, so that everyone can have a deeper understanding of the usage of js absolute value

The absolute value in js is not commonly used. I encountered it when I was writing a method today, so I recorded it and learned with everyone.

The default object in js - there is an abs function under the Math object, which is specially used to obtain the absolute value of a number, such as:

Copy code 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 as:
Copy code The code is as follows:
var aaa=-3;
var bbb=abs(aaa); //3

Example:
Copy code The code is as follows:

Another method:
We know that the absolute value in mathematics does not matter whether it is a decimal or an integer, and of course the same is true here.
Copy code The code is as follows:
var aaa=-3.3;
var bbb=abs(aaa); //3.3

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!