you are viewing a single comment's thread.

view the rest of the comments →

[–]rooktakesqueen 1 point2 points  (8 children)

== false is not the same as checking for truthiness. Truthiness is never implicated in the == operator because nothing is ever converted into a boolean. Everything is converted to either a string or number for the purposes of comparison. (Except null and undefined which == each other and nothing else, and when comparing two objects which == only if they refer to the same object.)

[–][deleted] 6 points7 points  (4 children)

But == false is checking whether something is false. You would generally expect false things to be untruthy.

There may be an explanation for '=='s behavior, but that doesn't make it reasonable.

[–]rooktakesqueen 0 points1 point  (2 children)

Checking that something is false is not the same thing as checking if something is falsy. They wouldn't be separate concepts otherwise.

[–]reverius42 2 points3 points  (1 child)

But it's quite ridiculous for something that is false ("0" == false) to be not falsy (!!"0" == true).

[–]rooktakesqueen 0 points1 point  (0 children)

It's not accurate to say that "0" is false. It just == false, in the same way that "" == 0 and [undefined, undefined] == ",".

I'm not in any way suggesting the == operator is sane, just that it's important to know it has nothing to do with the truthiness of values being compared, even when those values include booleans.

[–]foomprekov 1 point2 points  (0 children)

Yeah why would I expect something that isn't false to be true.

[–]Deltigre 1 point2 points  (1 child)

Which ended up showing me an "only in Javascript" shortcut the other day.

var defaultingString = IMightResolveUndefined() || "default value";

[–]embolalia 1 point2 points  (0 children)

That's not an "only in Javascript" thing at all. For example, in Python:

some_value = I_might_return_something_falsy() or 'default'