So basically I assume that there is something about equal/unequal that I didn't quite understand.
Asked
Active
Viewed 63 times
-4

Scott Marcus
- 64,069
- 6
- 49
- 71

helpspls
- 1
-
What is your question? – Scott Marcus Jan 26 '18 at 20:00
-
Answered here: https://stackoverflow.com/questions/1068834/object-comparison-in-javascript#1144249 – Mark Maurice Williams Jan 26 '18 at 20:01
-
`==` only looks at basic values, not complex object values. – Daniel A. White Jan 26 '18 at 20:03
-
3Please post text, not screenshots – Minzkraut Jan 26 '18 at 20:05
-
Possible duplicate of [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – Makyen Jan 27 '18 at 23:45
1 Answers
1
Your problem here is not really with the ==
or !=
operators, but rather the fact that in JavaScript no two objects are the same.
var obj1 = {
name: 'Joe'
}
var obj2 = {
name: 'Joe'
}
var obj3 = obj1;
console.log(obj1 == obj2); // false (2 separate objects)
console.log(obj1 == obj3); // true (pointing to the same object)
var primitive1 = 'aaa';
var primitive2 = 'aaa';
console.log(primitive1 == primitive2); // true (compared by value)
When you compare those objects, JavaScript is simply comparing by reference. You have created 2 different objects in memory and JavaScript compares non-primitives by looking at the reference only.

Sébastien
- 11,860
- 11
- 58
- 78