This question already has answers here:
Object comparison in JavaScript [duplicate]
(10 answers)
Closed 4 years ago.
When you comparing object with another object with same property why it returns false?
For Example
var person={
age:30
}
var person2={
age:40
}
console.log(person==person) or console.log(person===person)
it show's in console false why?
Objects are reference types, which means the equality operators operate on a reference to the object in memory, not to its contents.
In your particular case, you could serialize the object to a string and then check
const compareSerializableObjects = (a, b) =>
JSON.stringify(a) === JSON.stringify(b)
person === person will always return true as you are comparing the same reference, and if you are comparing person === person2 then it is a different refference which is false.
Did you mean person.age === person2.age ?
Related
This question already has answers here:
How to compare arrays in JavaScript?
(61 answers)
Closed 6 months ago.
I have an array which is called ids_to_remove. When I type it in browser inspect, I get the following result:
ids_to_remove
(2) ['1', '2']
When I type in the same array I get:
['1','2']
(2) ['1', '2']
When I compare the elements I get:
ids_to_remove[0]==='1'
true
ids_to_remove[1]==='2'
true
also
typeof(ids_to_remove[0])
'string'
But when I compare the arrays:
ids_to_remove===['1','2']
false
Does anyone have any idea why these two arrays are not equal?
Javascript arrays are objects and you can't simply use the equality operator == to understand if the content of those objects is the same. The equality operator will only test if two objects are actually exactly the same instance (e.g. myObjVariable==myObjVariable, works for null and undefined too).
you can simply use this function:
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
This function only works for simple non primitive type arrays.
This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Why is `Object() === new Object()` equal to `false`? [duplicate]
(1 answer)
Closed 3 years ago.
I have a variable defined as:
var o = new String("0");
In console when i write:
o === o
it returns true
but when i write:
new String("0") === new String("0")
it returns false
I don't understand why is it working on variable references but not on objects?
I tried it as:
(new String("0")) === (new String("0"))
because the problem may arise due to operator precedence, but it still returns false
new String("0") === new String("0")
Here you are comparing two different Strings having different references. Thats why you are getting false.
o === o
Here, You are actually comparing the same string (reference is same in this case).
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
This question already has answers here:
Check if a value is an object in JavaScript
(54 answers)
Closed 7 years ago.
Is it possible to test a variable to see if it is a primitive?
I have seen lots of questions about testing an variable to see if it is an object, but not testing for a primitive.
This question is academic, I don't actually need to perform this test from my own code. I'm just trying to get a deeper understanding of JavaScript.
To test for any primitive:
function isPrimitive(test) {
return test !== Object(test);
}
Example:
isPrimitive(100); // true
isPrimitive(new Number(100)); // false
http://jsfiddle.net/kieranpotts/dy791s96/
Object accepts an argument and returns if it is an object, or returns an object otherwise.
Then, you can use a strict equality comparison, which compares types and values.
If value was an object, Object(value) will be the same object, so value === Object(value). If value wasn't an object, value !== Object(value) because they will have different types.
So you can use
Object(value) !== value
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.