Why does {} == {} return false? [duplicate] - javascript

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 3 years ago.
I was writing a script, and I had something like
var myObject = {};
if (myObject == {}){
...
}
I was surprised to find that this evaluated to false.
My own findings
Reading up on some of the funny querks javascript does, I found this article, which tells that {} will result in a "truthy" object in an if statement, for example
if ({}){ // true
...
}
Hm, well further ready discuesses String Equality, which for object comparison, says it will return true if they reference the same object. (Even {} === {} returns false)
The only reason I can think that is would return false is that the objects are technically at different memory address, and thus considered different objects.

Because every {} creates a unique object. You can't compare them that way.

Related

Why does {} == {} equals false? [duplicate]

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 4 years ago.
Short question about JavaScript. I tried to execute {} == {} in JavaScript and expected to get true, but it didn't and I want to understand why. Shouldn't {} == {} return true and {} === {} return false?
Because == and === check if the two compared variables are references to the same object, not whether they are identical in value.
Thus a two variables internally referencing each other or a third variable are both == and ===, two new instances of an object are not.
To check if two objects are identical, you could JSON.stringify() them and check whether or not the results are the same.
Most common libraries for JavaScript contain a function to compare two objects, in vanilla JS you can make such a function for yourself:
Object.compare = function(obj1, obj2) {
if (obj1 && obj2) return JSON.stringify(obj1) === JSON.stringify(obj2)
}
console.log(
Object.compare({foo:"bar"}, {foo:"bar"})
);
When comparing two objects with ===, the references will be checked.
These are not two references to the same obejcts, these are two different instances of an empty object.
When comparing with ==, there might be usually some coercion to some common type prior to comparison, following specific rules that are too complicated to be listed here.
But long story short, since you are compariong two objects, there won't be a different check.

Why {} !== {} in Javascript [duplicate]

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 6 years ago.
I was going through Map Documentation on MDN. In Examples, under Using Map Object, Object Literal - {} is used as key to store value. But, the value in Map can't be retrieved using Object Literal.
I verified this in Browser Console and found that Object Literal is not equal to itself. Also, the Function Expression - function() {} is not equal to itself.
I couldn't find the reason behind this. If required, I can ask a different question for Function Expression.
Each time you do {}, it creates a new empty object, so when you do {} == {}, you're comparing two different objects. This comparison is done by reference, so it returns false.

Why is the equality operator on object returning false? [duplicate]

This question already has answers here:
Object comparison in JavaScript [duplicate]
(10 answers)
Closed 7 years ago.
I'm trying to manipulate the view of a web messaging system and the default view is a blank object, {}. I used an ng-show="data.currentView == {}" in my AngularJS script to check if it is currently the default view, but it never showed. Then I tried this in my Javascript Console in Chrome:
var data = {}
console.log(data == {})
//Logs false
console.log(data === {})
//Logs false
Why is that statement returning false?
You are testing to see if two objects are the same object, not if they are identical objects.
See the specification:
If Type(x) is the same as Type(y), then … Return true if x and y refer to the same object. Otherwise, return false.
You are comparing references with objects, so they are never equal. A good way would be:
Object.keys(data.currentView).length == 0

Why {} == {} is false in JavaScript [duplicate]

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Closed 8 years ago.
Why {} == {} and {} === {} is false in javascript?
{} == {} // false
{} === {} // false
javascript compares objects by identity, not value. Each object, each {} is distinct.
Same applies to arrays, too.
1) 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.so
{} == {} is false
2) it does not make any difference whether you use == or === for comparing objects, because comparing them always returns false.

Array comparison failure [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 9 years ago.
Long story short. Why is
console.log(obj.hello[0].w == ['hi','hi']); // false
in the following:
var obj = {
'hello':[
{'w':['hi','hi']}
]
}
console.log(obj.hello[0].w); // ['hi','hi']
console.log(obj.hello[0].w == ['hi','hi']); // false ??? Why is it false?
console.log(obj.hello[0].w[0] == 'hi'); // true
console.log(obj.hello[0].w[0] == ['hi']); // true
console.log(obj.hello[0].w[0] === ['hi']); // false
console.log(obj.hello[0].w[0] === 'hi'); // true
If obj.hello[0].w != ['hi','hi'], then what is the 'real' value of obj.hello[0].w?
EDIT: I first thought the problem was about JSON but it turned out it's about comparing objects. Sorry, for duplicate.
In addition to #MightyPork's answer, there are workarounds that are very simple.
The easiest way (supported by modern browsers) is to use JSON.stringify().
JSON.stringify(['hi', 'hi')) === JSON.stringify(['hi', 'hi')) // true
If your browser doesn't support JSON nativley you can include the JSON-js library on your page, which has the same syntax.
This isn't a complete solution. For example, functions always return null, except when they are class instances.
function a(){ this.val = "something"; }
JSON.stringify([a]) // "[null]"
JSON.stringify([new a]) // "[{"val":"something"}]"
You cannot compare arrays like if they were simple variables. You're comparing references to two different arrays, and that will always be false, regardless of their contents.
If you want to do the check, you will have to compare each value individually.

Categories