Home  >  Article  >  Web Front-end  >  How to compare sizes in javascript

How to compare sizes in javascript

藏色散人
藏色散人Original
2021-06-18 11:04:397181browse

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)){
}

Decimals can be like this.

if(parseFloat(xxx)>parseFloat(xxxx)){
}

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!

Statement:
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