Comparison does not work in Javascript with strict equal

I have below Basic Javascript to undo a number that works when I use

while (!no == 0)

But it doesn’t work when I use

while (!no === 0)

I tried in the console for parseInt(0) , which returns only number , and my no already number , so

why === doesn't work, can someone help and explain to me better?

 function findRepeated(number) { var a, no, b, temp = 0; no = parseInt(number); while (!no == 0) { a = no % 10; no = parseInt(no / 10); temp = temp * 10 + a; } return temp; } console.log(findRepeated(123)); 
+5
source share
2 answers

!no == 0 does not match !(no == 0) . This is (!n) == 0 .

Similarly !no === 0 - (!no) === 0 .
This is always evaluated as false because !no is a boolean and 0 is a number.
Values ​​of different types are never === .

Read about the logical NOT operator, comparison operators, and operator precedence .

+8
source

Types

!no returns a boolean, returns false, although the value no is a number, adding a negation operator before any number will return false, i.e. console.log(!123 === false) . As you know, using == converts the data type to you, where using === will not.

Example

I like the example of null and undefined. While null == undefined true, null === undefined is false, because null is an object type, while undefined is, of course, undefined.

 console.log("test1: " + !123); // false console.log("test2: " + (!123 === false)); // true console.log("test3: " + (!123 == 0)); // true console.log("test4: " + (!123 === 0)); // false console.log("test5: " + (0 == false)); // true console.log("test6: " + (0 === false)); // false console.log("test7: " + (null == undefined)); // true console.log("test8: " + (null === undefined)); // false console.log("test9: " + typeof null); // object console.log("test10: " + typeof undefined); // undefined 
+4
source

Source: https://habr.com/ru/post/1275418/


All Articles