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 ?

Alexander
  • 12,424
  • 5
  • 59
  • 76
  • 11
    You act as if there was no documentation on == – Denys Séguret Feb 19 '14 at 18:35
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators – Marc B Feb 19 '14 at 18:36
  • 1
    `==` is not transitive, `a == b` and `b == c` does not mean `a == c`. Algorithm in spec [**here**](http://es5.github.io/#x11.9.3); 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. – Paul S. Feb 19 '14 at 18:36
  • 1
    [Also the Ecma spec.](http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3) – Pointy Feb 19 '14 at 18:36
  • 3
    The `==` operator in Javascript is not transitive is the simple answer. – thatidiotguy Feb 19 '14 at 18:36
  • 1
    [das spec](http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3) – canon Feb 19 '14 at 18:36
  • You match too string that one has got no signs ("") and another that has got one sign ("0"->zero). I actully recommend you to use === and !== because == and != sometimes return wrong results. Like 0 == ''; is true – Blauharley Feb 19 '14 at 18:37
  • From the spec `d. 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.` – Xotic750 Feb 19 '14 at 18:39
  • @user2864740 How can you claim that when he has an example above illustrating that very fact? It is certainly transitive when the types are the same if that's what you are getting at. – thatidiotguy Feb 19 '14 at 18:46
  • @thatidiotguy I had a terminology fail :-/ – user2864740 Feb 19 '14 at 18:49
  • if both are strings (or the same type in general), they are compared without type conversion. ("" == 0). – dandavis Feb 19 '14 at 19:29

5 Answers5

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.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    Additional: from the spec [11.9.3 1.d](http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3) – Xotic750 Feb 19 '14 at 18:42
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

dpan
  • 5,352
  • 2
  • 26
  • 29
  • Sure, see [11.9.3 The Abstract Equality Comparison Algorithm](http://es5.github.io/#x11.9.3) - this outlines the exact rules Felix followed in his answer. – user2864740 Feb 19 '14 at 18:50
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. :)

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
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!')
Keith
  • 150,284
  • 78
  • 298
  • 434
  • *"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`). – Felix Kling Feb 19 '14 at 19:03
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.

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158