3

I was playing with javaScript and found something confusing..

var a = [1,2,3];
var b = [1,2,3];
var c = '1,2,3';

a==b // false
b==c // true
c==a // true

What is happening behind the scenes ? anybody know that ?

gauravmehla
  • 686
  • 6
  • 21
  • 5
    Possible duplicate of [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Esko Jun 26 '17 at 10:55
  • Which line is confusing? `a==b //false` or `b == c //true`? – Yury Tarabanko Jun 26 '17 at 10:58
  • Though [`b===c` returns `false`](https://jsfiddle.net/xpg3krzv/). [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Liam Jun 26 '17 at 10:58
  • 2
    Why not ask about `![] == [] // -> true?` – webdeb Jun 26 '17 at 10:59
  • b==c .. @YuryTarabanko – gauravmehla Jun 26 '17 at 10:59
  • Thanks for this @webdeb.. can you please tell me the answer also.. :| javascript is amazing... – gauravmehla Jun 26 '17 at 11:01
  • I mean [he has](https://stackoverflow.com/a/44758179/542251) answered this? – Liam Jun 26 '17 at 11:02
  • @gauravmehla You could read how `x == y` should be implemented [here](https://tc39.github.io/ecma262/2017/#sec-abstract-equality-comparison). Your `b == c` comparison corresponds to step 9. – Yury Tarabanko Jun 26 '17 at 11:04
  • @gauravmehla I updated my answer. – webdeb Jun 26 '17 at 11:08

3 Answers3

5

Evidently arrays get cast to comma delimited strings when compared to strings. [UPDATE: as Sawant correctly states, it's calling the array's toString() method.] But arrays compared to arrays simply remain as arrays, and only the same instance will be equal to itself.

This is a good reason why you should always use === and never use ==.

If you need to compare the arrays by the elements they contain, use the isEqual function from the lodash library.

David L. Walsh
  • 24,097
  • 10
  • 61
  • 46
3
  1. Comparing two objects (array), they are not the same!

    a==b // false
    
  2. Comparing two strings, because an array compared to a string, will be casted to a string.

    b==c // true
    
  3. The same as 2.

    c==a // true
    

When you compare with == the interpreter will try to make left-hand-side and right-hand-side the same type.

It will first format it to a string -> number -> boolean

So for example, comparing ![] == [].

  1. ![] == []
  2. false == []
  3. false == ""
  4. false == false

This are the steps the interpreter has to perform to come up with:

  ![] == [] // true

Because of all this, just use always the === operator, and it will be also much faster.

webdeb
  • 12,993
  • 5
  • 28
  • 44
3

Array compared to another array is not the same because they are pointing at different memory addresses.

But an array compared to a string for equality (b == c), casts the array to a string; effectively, it's doing something similar to:

b.toString() === c

But with an identity comparison (===), it will also check the type of both variables, in which case b === c will render as false.

Sawant
  • 4,321
  • 1
  • 27
  • 30