Home >Web Front-end >JS Tutorial >How to determine whether numbers are equal in js
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); // falseIf 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); // true3, underfined, null, 0, NaN, and "" will all become false when converted to Boolean values, then How does it perform in "=="?①Underfined and nullUndefined 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
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!