0

I have two samples of code that I don't understand why they evaluate the way they do

Sample one (these evaluate to true):

    {} + [] == 0
    {} + 1 == 1
    0/1 == 0

But then this will evaluate to NaN instead of 0:

    ({} + []) / ({} + 1)

Sample 2 (you can chain the first):

    1 == "1" == 1/1

But this will evaluate false even though choosing any two will evaluate true.

    0 == "0" == 0/1
  • 2
    Related https://www.destroyallsoftware.com/talks/wat – PeeHaa Apr 07 '14 at 22:14
  • 1
    try the first set with '===' instead of '==' then go read up on the difference. – rlemon Apr 07 '14 at 22:14
  • Another one for practice: `0==1==0` – dkasipovic Apr 07 '14 at 22:15
  • 2
    `{} + []` is interpreted as `+[]`. The `{}` is a "block", not an object. `[]` is converted first to a string (`''`), then to a number (`0`). It's not doing addition, it's the unary `+` operator. Same with `{} + 1`. When the `{}` is parenthesis, like in `({} + [])`, then it's treated as an object, and it becomes string concatenation (`'[object Object]' + ''`). – gen_Eric Apr 07 '14 at 22:15
  • Try `0 == {}+[]` and `1 == {}+1` instead – Bergi Apr 07 '14 at 22:20
  • @PeeHaa: As a JavaScript developer.... yep. – gen_Eric Apr 07 '14 at 22:30

3 Answers3

0

You got explanations for first sample, so here is about second:

1 == "1" == 1/1 -> true

Explained: 1 == "1" -> true, then true == 1/1 evaluates to 1 == 1 which is true

0 == "0" == 0/1

Explained: 0 == "0" -> true, then true == 0/1 evaluates to 1 == 0 which is false

That is why my example 0 == 1 == 0 in the comments, evaluates to true.

0 == 1 -> false, false == 0 -> true

dkasipovic
  • 5,930
  • 1
  • 19
  • 25
0

Sample one (these evaluate to true):

{} + [] == 0
{} + 1 == 1

Please understand the difference between your expressions and the following:

 ({} + []) == 0
 ({} + 1) == 1

Considering that the parenthesised expressions evaluate to "[object Object]" (and "[object Object]1" respectively), it will become clear why the division yields NaN.


Sample two is a different thing.

Are there special equality conditions relating to 0 in javascript?

Yes. 0 is the only falsy number, which makes a difference when comparing with booleans (where false will be cast to 0).

1 == "1" == 1/1

Check the evaluation of this:

(1 == "1") == 1/1 // casting the string to a number
(1 == 1) == 1
true == 1 // casting the boolean to a number
1 == 1
true

Since 1/1 is 1 and equality is commutative, the same holds for 1 == ("1" == 1/1)

But this will evaluate false even though choosing any two will evaluate true.

0 == "0" == 0/1

No "but":

(0 == "0") == 0/1 // again, casting the string to a number
(0 == 0) == 0
true == 0 // again, casting the boolean to a number
1 == 0
false

Again it doesn't matter whether we do (0=="0)==0 or 0==("0"==0)

Even without the typecasting, in plain boolean logic is clear that

(true == true) == true

will have a different result than

(false == false) == false

JavaScript does not support chained comparison operators, the operations will be implicitly nested. If you need to test three values for equality, do (false == false) && (false == false).

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
-1

When you use the == operator you allow coercion. That makes values either "truthy" or "falsy" depending on what operands are involved. Try it with the === operator to see what happens.

user1329482
  • 569
  • 3
  • 8
  • as I hit enter on my comment I saw your answer crop up :P nice. – rlemon Apr 07 '14 at 22:14
  • 1
    Using `===` for sample 1 won't make a difference actually – Bergi Apr 07 '14 at 22:21
  • As might be true, but I'm trying to figure out why someone would compare either an empty object or empty array to 1. Even in a loosely-typed language a programmer has to show some discipline--in fact, I'd say particularly in a loosely-typed language. – user1329482 Apr 08 '14 at 10:24