Reputation: 12785
I am trying to understand something here , best way to explain myself is by giving an example :
"" == false
// true
"0" == false
// true
false == false
// true
but what happens here ?
"" == "0"
// false
If ""
evaluates to false
and "0"
evaluates to false
the logic predicts that it is the same as i write false == false
.
i do realize that i am trying to compare two strings here , but how does the language knows the difference between "a" == "b"
or "" == "0"
? how does coercion happens in this case ?
Upvotes: 0
Views: 413
Reputation: 26968
Take a look on this specification,
If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
This explains everything.
Upvotes: 0
Reputation: 155652
This is due to the slightly odd way Javascript handles equality checks: you can check value with implicit type conversion with ==
or check type and value with ===
.
This also means that a list of 'falsish' values evaluate to false
if compared with ==
: ""
, 0
, undefined
, null
Now: "" == "0"
is comparing two strings that are obviously different.
The messy question is why does "0" == false
?
This is do do with the Javascript spec for ==
:
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
When you compare "0"
to a boolean it is first converted to a number, 0
, which is 'falsish'. When you compare "0"
to a string the strings are compared.
If you convert directly (without using ==
) then "0"
evaluates to true:
if("0") console.log('Now "0" is true!')
Upvotes: 0
Reputation: 816334
Why “” == “0” is false in javascript?
Because the operands are two strings with different content. Type coercion only takes place if the data types of the operands are different.
Related questions:
If
""
evaluates to false and"0"
evaluates tofalse
the logic predicts that it is the same as i writefalse == false
Lets have a look how the comparisons are actually resolved:
"" == false
is coerced to 0 == 0
"0" == false
is coerced to 0 == 0
false == false
: same data type, hence the values are directly compared
As you can see "0"
doesn't "evaluate" to false
, it is converted to an integer, and that value is compared. (""
does evaluate to false
(empty string) but when converted to a number, it is 0
).
There is a big difference between converting a value to a boolean and comparing a value to a boolean. Most obvious example: !!"0"
(true
) and "0" == false
(true
).
When you compare values of different datatypes with loose comparison (==
), they are always coerced to numbers (or potentially strings, if you are comparing an object with a string).
Have a look at the specification for more information about the comparison algorithm.
Upvotes: 12
Reputation: 5402
Both operands are treated as strings (since ""
is a string, "0"
is treated as string also) so the comparison returns false
.
Equal (==)
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
From Comparison Operators in Mozilla Developer Network
Upvotes: 1
Reputation: 6792
I tried the examples you gave and the interesting thing I found out is that 0 and "" are considered to be equal to false. I knew about 0 being represented as false, but I came to know about "" just now. Thanks to you. :)
Now, what I tried was comparing "" == 0 which gave me true. So here what happens is, since the two operands compared are not of the same data type, javascript converts them (coercion happens) and as you yourself checked and stated they both are the samething i.e. false and false == false is true. But when you try comparing "" == "0", cause of the double quotes around them they get treated as String's and as Strings they are not equal as their contents are different. This is the reason we get false there.
Thanks for the question, it wasn't the best but I tried and learnt something. :)
Upvotes: 0