About arrays coerced to strings in javascript

About arrays coerced to strings in javascript
0

#1

Here is one of the paragraph in YDKJS up and going.

For example, arrays are by default coerced to strings by simply joining all the values with commas (,) in between. You might think that two arrays with the same contents would be == equal, but they’re not:

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

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

Why a!=b? I thought a and b have the same elements and they are both array?


#2

JS arrays are objects. When you declare:

var a = [1,2,3];

JS creates a reference to a specific memory location.
When you declare:

var b = [1,2,3];

JS creates a reference to a different memory location

Because a and b two separate addresses in memory so they will not be equal.

Let’s say you have:

var a = [1,2,3];
var b = a;
a == b; // yields true;

Now, a has been assigned the same reference as b, they are evaluated as being true.


#3

Other people may try to explain the inconsistency - my take is to simply never ever use double-equals

always use triple-equals strict equality ===

I wrote about array comparison here