Why a == b is false | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why a == b is false

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

9th Apr 2020, 10:14 PM
BangArray
BangArray - avatar
7 Answers
+ 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));
10th Apr 2020, 12:50 AM
visph
visph - avatar
+ 2
It is different objects, then why it is should be true?
9th Apr 2020, 10:50 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 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
11th Apr 2020, 3:44 PM
Nahom Tesfamichael
Nahom Tesfamichael - avatar
+ 1
But values of a n b are same
10th Apr 2020, 12:16 AM
BangArray
BangArray - avatar
+ 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
10th Apr 2020, 10:57 PM
Abhay
Abhay - avatar
0
Nahom Tesfamichael -nice one👍
11th Apr 2020, 3:46 PM
BangArray
BangArray - avatar
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('')
11th Apr 2020, 10:04 PM
Levizar
Levizar - avatar