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

How to compare sizes in javascript

藏色散人
Release: 2021-06-18 11:04:39
Original
7160 people have browsed it

How to compare sizes in javascript: 1. Use parseInt to compare the sizes between integers; 2. Use parseFloat to compare the sizes between decimals.

How to compare sizes in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to compare sizes in javascript?

js Compare the size of numbers or decimals

JS must not compare directly when comparing numbers.

But it needs to be converted, because var in JS is a weak type. The default is string, and errors will occur during all comparisons.

There are two conversion methods in JS, parseFloat and parseInt.

When comparing, think of the following.

Integers are as follows:

if(parseInt(xxx)>parseInt(xxxx)){
}
Copy after login

Decimals can be like this.

if(parseFloat(xxx)>parseFloat(xxxx)){
}
Copy after login

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to compare sizes in javascript. For more information, please follow other related articles on the PHP Chinese website!

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!