0

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 ?

11
  • 11
    You act as if there was no documentation on == Commented Feb 19, 2014 at 18:35
  • 1
    == is not transitive, a == b and b == c does not mean a == c. Algorithm in spec here; The bits you want are: If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y. and compare this against If Type(x) is the same as Type(y), then If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false. Commented Feb 19, 2014 at 18:36
  • 1
    Also the Ecma spec. Commented Feb 19, 2014 at 18:36
  • 3
    The == operator in Javascript is not transitive is the simple answer. Commented Feb 19, 2014 at 18:36
  • 1
    das spec Commented Feb 19, 2014 at 18:36

5 Answers 5

12

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 to false the logic predicts that it is the same as i write false == 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Additional: from the spec 11.9.3 1.d
1

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

1 Comment

Sure, see 11.9.3 The Abstract Equality Comparison Algorithm - this outlines the exact rules Felix followed in his answer.
0

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. :)

Comments

0

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!')

1 Comment

"This also means that a list of 'falsish' values evaluate to false if compared with ==: "", 0, undefined, null" I would be very carful with the wording here. None of those values "evaluate" to false in the comparison. If any, they are converted to numbers (as you mention later). And there are even special cases for undefined and null in the algorithm (although, they might actually be converted to numbers as well, in which case the result is NaN).
0

Take a look on this specification,

http://es5.github.io/#x11.9.3

If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.

This explains everything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.