Home > Web Front-end > JS Tutorial > How to determine whether numbers are equal in js

How to determine whether numbers are equal in js

藏色散人
Release: 2023-01-03 09:26:41
Original
5573 people have browsed it

JS method to determine the equality of numbers: first create an HTML sample file; then add a script tag; finally determine the number through "document.getElementById("demo").innerHTML = (x == 8);" Whether they are equal or not is enough.

How to determine whether numbers are equal in js

The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

In JavaScript, you can directly use the comparison operator "==" to compare whether two numbers are equal. When comparing numbers with numbers, you cannot put 0 in front of it because 0 represents an octal number in the program.

1. JavaScript determines whether the numbers are equal.

console.log(012==12);  //false
console.log(012==10);  // true
console.log(099==99);  //true 这种情况是因为八进制中不可能出现9,所以看成一个十进制
console.log(09==9);    //true 同上
Copy after login

Example:

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript比较</h1>
<p>把 5 赋值给 x,然后显示比较 (x == 8) 的值:</p>
<p id="demo"></p>
<script>
var x = 5;
document.getElementById("demo").innerHTML = (x == 8);
</script>
</body>
</html>
Copy after login

Running results:

How to determine whether numbers are equal in js

##[Recommended video tutorial:

js basic tutorial]

JavaScript compares whether two values ​​are equal:

2. Under normal circumstances, convert both sides into number type data as much as possible, and then compare, rather than converting to Boolean type

console.log(true==2);   // false
Copy after login

If both sides are converted to Boolean type and then compared, then true==true and false will not be returned, so it proves that it is not both sides that are converted to Boolean type and then compared! ! It should be that both sides are converted to number type, 1==2, and false

console.log(true==1);   // true
Copy after login

3, underfined, null, 0, NaN, and "" will all become false when converted to Boolean values, then How does it perform in "=="?

①Underfined and null

Undefined and null return false when compared with any meaningful value. Null and undefined are equal to other numbers in the operation No type conversion is performed, but null==undefined

console.log(null==undefined);  //true  
console.log(null===undefined);  //false ===: 全等:不仅比较值是否相等,数据类型要相同
console.log(null==0);          // false
console.log(undefined==1);     //false
console.log(null==false);       // false
console.log(undefined=="");     // false
Copy after login

The above is the detailed content of How to determine whether numbers are equal in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
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