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.
console.log(012==12); //false console.log(012==10); // true console.log(099==99); //true 这种情况是因为八进制中不可能出现9,所以看成一个十进制 console.log(09==9); //true 同上
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>
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 typeconsole.log(true==2); // false
console.log(true==1); // true
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
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!