0

Javascript boolean tricky question, Either You can explain why? or welcome to the confusion club.

I don't know if it defines as a tricky but I asked few guys and they all had no idea why it is happening. here is the basic noob code. var name = ""; var status = ""; var m = "asd"; console.log(Boolean(name)); // or status, both are false coz both empty string console.log(Boolean(m)); // this is true coz a filled string. console.log((name == status) == m); // So the way js works, as JS being asynchronous. it should be like this. console.log((true) == m) according to grouping operator, first they will be checked, and then now it's (true == m) and if you check what is m in boolean, u can see it's true by Boolean(m) func. so it should be (true == true) which should return the true value. But it is returning false HOW? (name == status); // returns true. coz both are false. // avoiding === so it doesn't make it more confusing ps: I found this while practising with booleans and didn't get an explanation yet.

23rd Jun 2021, 5:20 PM
eminem
eminem - avatar
6 Answers
+ 3
"If one of the operands is Boolean, convert the Boolean operand to 1 if it is true and +0 if it is false." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality console.log((name == status) == m); console.log((true) == m); console.log((1) == m); console.log((1) == "asd");// false
23rd Jun 2021, 7:03 PM
ODLNT
ODLNT - avatar
+ 4
ODLNT u cleared my doubt thank you so much. I just put var m = 1; And it finally returned true, I now understand it clearly than ever. Thanks.
23rd Jun 2021, 7:10 PM
eminem
eminem - avatar
+ 2
and btw console.log(true == true == true); // returns true, in case anybody say, comparing 3 values will return false always. btw, I just realized that when you compare objects it will always return false, and I read that almost everything in JS is an Object. is this the reason???
23rd Jun 2021, 5:22 PM
eminem
eminem - avatar
+ 2
Dror Krief i just tried name == name == m // returns false. name == name // it's true, but then m is also true. Then why it's not true == true // A BIG true?
23rd Jun 2021, 6:54 PM
eminem
eminem - avatar
+ 2
You're welcome, I'm glad I was able to help.
23rd Jun 2021, 7:14 PM
ODLNT
ODLNT - avatar
+ 1
very interesting true == m // is true what if you do something like name == name == m ?
23rd Jun 2021, 5:56 PM
Dror Krief
Dror Krief - avatar