Why b?not a? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 6

Why b?not a?

var a = new String("hello"); var b = new String("hello"); if(a === b) { alert("a"); } else { alert("b"); }

1st Sep 2018, 4:24 PM
pro
pro - avatar
6 ответов
1st Sep 2018, 4:44 PM
Théophile
Théophile - avatar
+ 3
because 'a' is only a reference to an object and 'b' also. They have the same values (a and b), right, but the object exists 2 times, and both have a different adress in the RAM. Like you compare if(0xff6384 === 0xaa6384) I would say 🤔 But maybe I'm wrong! Whats also funny, try to compare [] with [] 😂 if([] == []){ //this will never executed }
1st Sep 2018, 5:48 PM
BraveHornet
BraveHornet - avatar
+ 1
A and b aren't the same object. Using ==, the output is "a".
1st Sep 2018, 4:41 PM
Théophile
Théophile - avatar
+ 1
Sorry, using ==, it doesn't work... 🤔I will try to give you a correct answer...
1st Sep 2018, 4:42 PM
Théophile
Théophile - avatar
+ 1
thanks a lot ^_^, l was comparing objects:D
1st Sep 2018, 4:47 PM
pro
pro - avatar
+ 1
The content is equal but the object is different. Use: (a.valueOf === b.valueOf)
3rd Sep 2018, 2:21 AM
Mr Genesis
Mr Genesis - avatar