The var type defined in JavaScript is a weak type, and the default is String type. When comparing the size of two numbers, the default comparison is two strings
You cannot directly use ">" and "<" in js to directly determine the size
For example, when comparing 10 and 2, compare 10 by number It is greater than 2, but when compared according to the default string comparison, the first 1 and the first 2 are compared to 2, which will cause the phenomenon that 2 is greater than 10.
So when comparing the sizes of two numbers in javascript, you need to convert the types before comparing.
For example:
if(3>4) should be written as
if(eval(3)>eval(4)) var n=4;
if(n>3) should be written as
if(parseInt(n)>eval(3)) var n=4;
if(3 Thank you all for reading, I hope you will benefit from it. This article is reproduced from: https://blog.csdn.net/CPLASF_001/article/details/89455277 Recommended tutorial: "JS Tutorial" The above is the detailed content of Do you know how to compare numerical values in js?. For more information, please follow other related articles on the PHP Chinese website!if(eval(3)<parseInt(n)&&parseInt(n)<eval(5))