This question already has answers here:
Comparing objects in JavaScript
(10 answers)
Closed 8 years ago.
So the question is that two same objects are not equal in JavaScript, let's say:
Object() == Object()
or even:
[{a: 1}, {a: 2}].indexOf({a: 1}); //returns -1 not found
What's the reason of this strange behavior?
Objects are compared by reference. And two references are equal only if they point to the same object.
Objects are reference and when you compare two reference they return false.
The other answer(given by Eamon Nerbonne) here has a very relevant point:
Objects are considered equivalent when
They are exactly equal per === (String and Number are unwrapped first to ensure 42 is equivalent to Number(42))
or they are both dates and have the same valueOf()
or they are both of the same type and not null and...
they are not objects and are equal per == (catches numbers/strings/booleans)
or, ignoring properties with undefined value they have the same properties all of which are considered recursively equivalent.
Same applies for Arrays
([] === []) //returns false
And NaN is a special value as well which is never equal to itself.
NaN === NaN //False
Related
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Why does this index of a two dimensional array return -1 [duplicate]
(2 answers)
Closed 3 years ago.
I have simple nested array, like:
var arr = [[75.0], [65.0]] ;
and when I do:
arr.indexOf( [75.0] );
I expect 0, but I get -1, what gives?
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf :
indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).
There is the problem. In your example, you have an array of arrays. Comparing with === operator means that for it to evaluate to true, it has to be the same array object. Clearly it is a different object so it is not found from the array.
You need to use Array.find() instead where you can provide the testing function. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
This question already has answers here:
How to check if two arrays are equal with JavaScript? [duplicate]
(16 answers)
Closed 6 years ago.
I want to know why v.map(...) is not equal to v.map(...) using the same function to do the map
var v = [1,2,3,4,5]
v.map(function(a) {return a+1}) === v.map(function(a) { return a+1;})
Running this on node repl, I've got the result of the second expression as false
When comparing the equality of non-primitives, Javascript checks equality by equality of reference - that is, do both objects being tested refer to the same object in memory?
In this case, they do not - .map returns a new object, and running .map on the same array two different times will return two different references - regardless of the computations done while mapping.
To sum it all up:
v === v is true because v refers to the same object in memory
[1,2,3,4,5] === [1,2,3,4,5] is false because both arrays are different objects in memory (i.e. reference is compared, not values)
.map will always return a new instance of an array - so the result cannot be equal to another .map statement
This is because arrays are compared by reference, not by value, in Javascript.
In other words, doing something like
[1,2] === [1,2];
returns false in Javascript because the two arrays occupy different locations in memory. They are not compared by the actual contents they may have (which is what we would call comparison by value).
In contrast, doing
var x = [1,2];
x === x;
returns true. In this case, the variable x refers (resolves to the memory location of the array) to the same array and this common reference to a memory location is sufficient to yield true.
map() returns an array. Comparing two arrays by value is a fairly complicated affair in Javascript.
This question already has answers here:
What’s the difference between "Array()" and "[]" while declaring a JavaScript array?
(19 answers)
Closed 8 years ago.
What is the correct way on instantiate empty array in javascript?
var array=new Array();
var array=new Array;
var array=[];
All of them look same. Is there any difference?
var array=[];
works best.
Ref - http://jsperf.com/array-instantiation
There is one more way:
var array = Array() ;
They all will produce the same result, an empty object Array. I think the best way is using the array literal shortcut [] as it is what use less characters, it is what most people do.
Note that the same does not apply in the case of Boolean, Number and String, as those constructors will return primitives of type 'object' (object Boolean, object Number and object String respectively) while their literals will return primitives of types 'boolean', 'number' and 'string' respectively (and will behave differently in some situations), but array primitive type it is 'object' in any case.
This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Closed 8 years ago.
I run the following in console why is the output false. Not asking how to compare two objects but why these two objects are not same.
> a = {same:'same'}
Object {same: "same"}
> b = {same:'same'}
Object {same: "same"}
> a === b
false
> a == b
false
Two objects are never the same even if they have the same content, as two different instances of Object is never equal.
When comparing two object, JavaScript compares internal references which are equal only when both operands refer to the same object in memory, keys and values are not checked, so the content of the object doesn't matter, the operands both have to reference the same object to return true in a comparison.
This is simply due to how == is defined per the The Abstract Equality Comparison Algorithm:
1. If Type(x) is the same as Type(y) [i.e. Type(x) == Type(y) == Object], then ..
1.f. Return true if x and y refer to the same object. Otherwise, return false.
None of the other rules/conversions apply because both operands are Objects.
Although there is no ECMAScript 5th edition "core" support for this task, several solutions are discussed in How to determine equality for two JavaScript objects?
This has naught to do with "references", which are an implementation detail not defined in ECMAScript, and can be entirely discussed per the above rules: two different Objects are never equal per == (and by extension ===) rules.
You are comparing two objects which never be equal. If you compare a.same and b.same then they will be the same.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you determine equality for two JavaScript objects?
I want to check if two objects not different :
var v1 = {id:"llll", dd="kkkk"};
var v2 = {id:"llll", dd="kkkk"};
if (v1 == v2)
{
alert("lll");
}
not work why ????
Because objects are compared by reference:
Functions
Objects (Host objects, native objects/constructors and instances)
A common instance is {}, which is similar to new Object.
The following object types are compared by value, not by reference:
Strings
Numbers
Booleans
null and undefined
Additionally, there's one object which is never equal to itself, not even by reference:
var test = NaN;
alert ( test == NaN ); // false
alert ( test == test ); // false (!)
To check whether two objects are equal, you have to define equality:
"Two objects are equal if they contain the same property names and values"
Which implies that object A has to have the same number of properties as object B, and that every property in A has also to be a property of B.
Try using "===" instead of "==" to compare the objects.