Why a == b is false | SoloLearn: Learn to code for FREE!


Why a == b is false

var a =[ ]; var b =[ ]; console.log(a==b) returns false.. Why why wht


4/9/2020 10:14:58 PM


7 Answers

New Answer


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));


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


It is different objects, then why it is should be true?


But values of a n b are same


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


Nahom Tesfamichael -nice one👍


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('')