Why my two arrays with string elements are not equal? [duplicate] - javascript

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.

Related

Javascript comparing object [duplicate]

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 ?

Why doesn't anArray === [] evaluate to true when anArray.length === 0 does in Javascript? [duplicate]

This question already has answers here:
Triple equal signs return false for arrays in javascript. why?
(4 answers)
Closed 6 years ago.
Can you explain this weird JavaScript behavior?
First :
[] === [] false
[] == [] false
Why false? The object are identical, thus it should return true.
Second :
[] !== [] true
[] != [] true
Again, why true? the objects are identical.
They're not identical. Object identity is defined by both operands pointing to the same instance.
var a = [],
b = [];
a == b; // false
a == a; // true
Two literals always evaluate to two different instances, which are not considered equal. If you are looking for structural equivalence, see How to compare arrays in JavaScript?.
Objects are not identical. In this case you compare the references to the objects. Easily speaking you compare the addresses in memory where these objects are located. This rule doesn't relate to primitives where you compare the actual values.

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.

Javascript - arrays comparison [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Javascript: Equality Comparison between two Object/ Array
(3 answers)
Closed 8 years ago.
var array1=[1,2,3];
var array2=[1,2,3];
alert((array1<array2)+(array1==array2)+(array1>array2));
As alert returns 0, array1 is not greater, not less and not equal to array2.
The question is:
How does array1 relates to array2?
Edit: my question is: How does array1 relates to array2?
The two array array1 and array2 are never equal as they are different instances.
If you want to compare them, you can do:
array1.join() == array2.join() // true
And BTW, the alert() doesn't alert false it alerts 0
Weird thing about arrays, they are never equal since they're not the same object.
Although, arrays act strange when you compare them with < or >
We can use this weird behavior to our advantage to check whether two arrays have the same content:
function arraysEqual(a, b) { return !(a < b || b < a); }
Or
function arraysEqual(a, b) { return !(a < b) && !(b < a); }
Which means, if the no array is bigger than the other, they're equal.
Source: How to check if two arrays are equal with JavaScript?

Weird behavior of comparison operator JavaScript when using empty array [duplicate]

This question already has answers here:
Triple equal signs return false for arrays in javascript. why?
(4 answers)
Closed 6 years ago.
Can you explain this weird JavaScript behavior?
First :
[] === [] false
[] == [] false
Why false? The object are identical, thus it should return true.
Second :
[] !== [] true
[] != [] true
Again, why true? the objects are identical.
They're not identical. Object identity is defined by both operands pointing to the same instance.
var a = [],
b = [];
a == b; // false
a == a; // true
Two literals always evaluate to two different instances, which are not considered equal. If you are looking for structural equivalence, see How to compare arrays in JavaScript?.
Objects are not identical. In this case you compare the references to the objects. Easily speaking you compare the addresses in memory where these objects are located. This rule doesn't relate to primitives where you compare the actual values.

Categories