Twitter | Search | |
Mike Sherov πŸš€ May 15
Me: β€œJS is weird that it has different logic for if(x) and x==true” Dev twitter: β€œDon’t use ==β€œ. Me: β€œyes, I know, but why is there a difference?” DY: β€œhere’s how it happens!” Me: β€œyes, I understand how it works, but why?” DT: *10 likes on β€œdon’t use ==β€œ Me: β€œ...”
Reply Retweet Like
Adam Rackis May 15
Replying to @mikesherov
This pissed me off ages ago, when I was first learning JS. Didn't even Eich reply to your original tweet and admit it was a bit of a mistake? Or am I misremembering that?
Reply Retweet Like
Mike Sherov πŸš€ May 15
Replying to @AdamRackis
He did, basically saying he cribbed from Perl 4 and Borland C. But then we need to go a level deeper. What did the designers of Perl 4 see when they decided Boolean == (any) should cast Boolean to Number?!
Reply Retweet Like
Gorgi Kosev May 15
... another guess there was probably a moment in the design of JS where thought for a couple of seconds about "eq" and went "well do we really need another operator we can just special-case comparison of the other primitives, all other cases will coerce to string"
Reply Retweet Like
BrendanEich
No. I am a C hacker so Mocha after ten days had == like ===: if types of operands differed, false. Early inside-Netscape adopters on "Livewire" team whined for magic implicit conversion evil, so HTTP status "404" == 404. Idiot-me gave them what they wanted. D'oh! Learn, children!
Reply Retweet Like More
Mike Sherov πŸš€ May 15
I think β€œ404” == 404 is reasonable. Not idiotic. I’m specifically question Boolean vs. (any) performing toNumber on Boolean instead of toBoolean on (any). Maybe because that would lead to NaN == false, which otherwise, NaN never equals anything?
Reply Retweet Like
BrendanEich May 15
You're right, they are different. But the crucial problem child is '' => 0. I wrote "Unix won (see upthread)" referring to atoi(""). Perl did same. And Perl 4/5 returns true (1) for '1 == ""'. The '90s, I tell ya! ;-)
Reply Retweet Like
BrendanEich May 15
More serious point: if you like "404" == 404 but also "404.0" == 404, you broke transitivity, a property of all equivalence relations. "404" != "404.0". JS does but your tweet without further qualification does too, and boolean or 0 are not involved.
Reply Retweet Like
BrendanEich May 15
("JS does" => "JS does so break".)
Reply Retweet Like
Mike Sherov πŸš€ May 15
Well I don’t like it. I meant reasonable compared to toNumber(Boolean) and how that makes == x and if(x) different. With toNumber(Boolean) you also have plenty of things that are != false and simultaneously != true, which to me is less error prone but certainly more head scratchy
Reply Retweet Like
Mike Sherov πŸš€ May 15
e.g. I think a truth table were everything either == true or == false is least surprising:
Reply Retweet Like
BrendanEich May 15
ToNumber(Boolean) does make false into 0, you're right. But ToBoolean('') => false as well, so please widen your scope!
Reply Retweet Like
BrendanEich May 15
What I implemented in ten days. I hope someone reads and learns for "next time". Cheers!
Reply Retweet Like
Mike Sherov πŸš€ May 15
Yeah, I get it. Mind blowing you did it in 10 days and the opportunity you created for us all.
Reply Retweet Like
Mike Sherov πŸš€ May 15
'' == 0 already without any imagined tweaks. I’m not sure what narrowed scope I’m looking at that you aren’t?
Reply Retweet Like
BrendanEich May 15
You were talking about if (x) vs. if (x == true), not '' == 0.
Reply Retweet Like
BrendanEich May 15
The weakening of == via implicit conversions came after the first ten days. Alas!
Reply Retweet Like
Mike Sherov πŸš€ May 15
Yes, I was. How does '' == 0 fit into that discussion? I only mentioned it because you brought it up.
Reply Retweet Like
BrendanEich May 15
The question is should '' convert to false implicitly? If yes, JS! If no, why not? Answer does not relate to ToNumber(Boolean). Right?
Reply Retweet Like
Mike Sherov πŸš€ May 15
Right. Not related. So unsure why you brought it up as the problem child. All I was saying is the potential downside in an imagined change in == when a Boolean is one of the comparators is that now NaN == false. But the upside is that everything either == true or == false.
Reply Retweet Like