+ 1
Why a == b is false
var a =[ ]; var b =[ ]; console.log(a==b) returns false.. Why why wht
7 ответов
+ 7
But arrays are objects and so are compared by reference ^^
If you want to compare by values, you could do:
var json = JSON.stringify;
console.log(json(a)==json(b));
+ 2
It is different objects, then why it is should be true?
+ 2
Let me =[]; // so me is not you
Let you= []; // you ar not me
Console.log(a ==b) //me is euqall to you
// that is not equall so false
Correct if i am wrong guys
+ 1
But values of a n b are same
+ 1
BangArray yea you are right ,was wondering same ,quite confusing for me as I just started learning js ,it's really weird but as visph said this is how it actually works
0
Nahom Tesfamichael -nice one👍
0
As said before, it's false because despite these are 2 empty arrays, they aren't the same array. They are different like you and me are different despite the fact that we are both human.
An easy way to make the comparison is to join the arrays like this:
a.join('') === b.join('')