Home > Article > Web Front-end > How to compare sizes in javascript
How to compare sizes in javascript: 1. Use parseInt to compare the sizes between integers; 2. Use parseFloat to compare the sizes between decimals.
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!