$(document).ready(function(){
var obj1 = { }
var obj2 = { }
if(obj1 === obj2)
{
alert('true');
}
else
{
alert('False');
}
});
Why if(obj1 == obj2) returns false eventhough obj1 and obj2 are objects of same type?
$(document).ready(function(){
var obj1 = { }
var obj2 = { }
if(obj1 === obj2)
{
alert('true');
}
else
{
alert('False');
}
});
Why if(obj1 == obj2) returns false eventhough obj1 and obj2 are objects of same type?
Because obj1 and obj2 do not refer to the same object. Here's a nice tutorial where "Method 1" calls this out.
https://www.geeksforgeeks.org/java-equals-compareto-equalsignorecase-and-compare/
== is a shallow comparison, like shallow copy, true only if the refrences are identical, not the objects. http://adripofjavascript.com/blog/drips/object-equality-in-javascript.html JAVA and C++ also have this sort of limitation unless you overload the == operator or use a method, like compareTo, equals, and JAVAScript isEqual. JAVA Sets use object.equals(), which is furstrating as you cannot override with your own class.equals().
== is a shallow comparison
Some spectacular Java nonsense here.
The == operator cannot be overloaded and checks for referential identity. If two variables contain the same reference then by definition they refer to the same object, not to two objects, so the nonsense about "shallow comparison" is not just wrong; it's meaningless.
The remarks about equals
in Sets is also completely wrong. Sets use Objects.equals(a, b)
. If you bothered to check the API reference before posting you would have seen " equality is determined by using the equals method of the first argument."
Lots of references on the dangers and costs of various equalities. This one seems very nice: https://www.mattzeunert.com/2016/01/28/javascript-deep-equal.html
Which is it where, Objects.equals or the equals of the first object or ==?
== is referential equality. Two references refer to exactly the same object. This is different from shallow or a deep equals which are used to test some kind of equivalence between two different objects.
equals
in Java could use referential equality (as in the Object class) or any other logic based on the state of the objects being compared depending on the whim of the developr of the class... deep or shallow or inbetween.
The equals method in the Objects class (NB Objects
, not Object
) is a convenience method that provides null safetly for testing equality as defined by the first parameter (with the exception that a null parameter only returns true if both parameters are null).
Use of Objects.equals in the Java Set class
https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/Set.html#contains(java.lang.Object)
Objects.equals(a,b)
is defined as being a.equals(b)
with null safety
https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/Objects.html#equals(java.lang.Object,java.lang.Object)
Somehow, the last time i added an equals() to a JAVA List (not Set, as Set does not support lookup like List and Map), it got ignored. I wrote my own findXXX(). Your results may vary! I'd recheck, but it was in a student's work. I should write something to try again! Still not sure why Set does not want to support lookup. I suppose it meets some math ideal!
Set is just an interface somewhere in the middle of the Collection hierarchy that includes various kinds of List and unordered Sets. and iteration/streaming operations thereon. Similarly the Map interface covers all kinds of key/value structures with search/lookup operations.
If you hit an issue trying to do lookups or searches it's 99% sure you should chose another class in Collection or Map that directly supports your requirement.
In JS, even if the Object looks same, they can not equate to equality because of the being the reference type.
I like to solve this with somple trick as with JSON.stringify
$(document).ready(function(){
var obj1 = { };
var obj2 = { };
if(JSON.stringify(obj1) === JSON.stringify(obj2)) {
alert('true');
} else {
alert('False');
}
});
The fact of checking for referential equality has to do with the fact that you are comparing two objects, not whether you use strict equality ===
or loose ==
.
Strings and integers are compared by value, but objects are compared by reference...as in their location in memory.
To compare two objects you have to loop through the properties and compare the values.
But then you have issues like if the value is an object itself or is NaN or the two objects have different properties
Lodash is a good library that can help you handle all of that using _.isequal
method..if you don't mind including a library.
Or you can still compare by hand and step through your specific problem...but there's no easy snap in solution for this.
Your code uses === but your question only asks about ==.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.