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.

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.
1 2 3 4 | console.log(012==12);
console.log(012==10);
console.log(099==99);
console.log(09==9);
|
Copy after login
Example:
1 2 3 4 5 6 7 8 9 10 11 12 | <!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:

##[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
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
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
1 2 3 4 5 6 | console.log(null==undefined);
console.log(null===undefined);
console.log(null==0);
console.log(undefined==1);
console.log(null==false);
console.log(undefined== "" );
|
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!