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

Do you know how to compare numerical values ​​in js?

烟雨青岚
Release: 2020-07-15 13:24:32
forward
5593 people have browsed it

Do you know how to compare numerical values ​​in js?

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;
Copy after login

if(n>3) should be written as

if(parseInt(n)>eval(3))
 
var n=4;
Copy after login

if(3

if(eval(3)<parseInt(n)&&parseInt(n)<eval(5))
Copy after login

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!

Related labels:
js
source:csdn.net
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!