Closed gareys closed 6 years ago
This may be relevant since thinking that those values are converted to booleans in those comparisons would be inconsistent when comparing stuff like ( "0" == false ) and ( " \n\t " == false). They both return true even though Boolean("0") and Boolean(" \n\t ") are true.
That section has nothing to do with ==
. It is about converting to boolean type. I.e. what happens if you put such values as the condition to if
.
@marijnh the point we are trying to make is that your use of "" == false
as an example of converting to Boolean type is not an example of converting to Boolean type. It is actually coercing both values to
a number. I provided the ECMAscript spec and some more details to explain why this happens, but this example doesn't really go along with your topic of Boolean coercion.
That's not an example of converting to boolean, it's an example of ==
not doing what you want. I can see how it can confuse people. I'll take another look at it.
Thank you, @marijnh
Does patch 69e8e8e improve things for you?
Awesome! Thanks @marijnh
@marijnh this looks great.
I mentioned this in gitter, but realized this is probably a better place to discuss things.
While reading chapter 1 in the 3rd edition, a reader in our Dev Book Club discord server pointed out that the statement below is not entirely true.
Reference: https://eloquentjavascript.net/01_values.html#p_N4OuWeYOwF
Focusing on the part about
"" == false
we found there is a lot more going on.In this case, the right hand side of the equality comparison,
false
, is first coerced to0
. Then, the left hand side""
is coerced to0
. Then, they are compared. the final form of"" == false
is+0 == +0
.While in most cases,
""
(empty string) is coerced to a Booleanfalse
value, as mentioned in 7.1.2 of the ECMAscript spec. It is not coerced to a Boolean in the comparison to false as seen in the comparison expression"" == false
. So it is not an example of Boolean coercion as used in your statement in Chapter 1.Here are my sources: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf Sections 7.1.2, 7.2.13 https://stackoverflow.com/a/42770966