Why does this javascript return false? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why does this javascript return false?

var a={value:20}; var b={value:20}; alert(a==b); They're both the same, so shouldn't it return true?

22nd Apr 2017, 10:38 AM
Christopher Moran
5 Answers
+ 26
You can't compare objects...
22nd Apr 2017, 10:45 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 7
"The reason for this is that internally JavaScript actually has two different approaches for testing equality. Primitives like strings and numbers are compared by their value, while objects like arrays, dates, and plain objects are compared by their reference. That comparison by reference basically checks to see if the objects given refer to the same location in memory." src: http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html
22nd Apr 2017, 10:54 AM
Barış Can Ceylan
Barış Can Ceylan - avatar
+ 5
sees title... BECAUSE IT CANT C#!!! 😂😂😂 honestly have no clue.... dont even know javascript, just wanted to make a pun
23rd Apr 2017, 4:49 AM
DeleteThisAccount
+ 1
Short and sweet. Thanks.
22nd Apr 2017, 10:47 AM
Christopher Moran
+ 1
Comparing objects ALWAYS returns false. If you want to compare the objects values, you have to write a function of your own.
22nd Apr 2017, 11:57 AM
Thanh Le
Thanh Le - avatar