This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 7 years ago.
why is that? I assumed it's some implicit type conversion at first, but [] == [] is also false.
Arrays in javascript are Objects. Objects are compared by identity. So no two objects created by different literals (or by other means) are going to be equal (either strictly (===) or loosely (==)).
Related
This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
Why doesn't equality check work with arrays [duplicate]
(6 answers)
Object and primitive type equality
(5 answers)
Why isn't [1,2,3] equal to itself in Javascript? [duplicate]
(6 answers)
Closed 2 years ago.
Is it significant to an understanding of variables, memory, of primitive v.s. referential data-types that samely-typed objects can never be considered 'the same' but samely-typed primitives can?
console.log(5 === 5);
console.log({num: 1} === {num: 1});
//I'm aware that:
o = {num: 1}
o2 = o;
console.log(o === o2);//returns true
P.s. Genuine question, am hoping to get a better core understanding of JS as it seems to help so much later on to have this.
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 compare arrays in JavaScript?
(55 answers)
[] is not identical to [] [duplicate]
(4 answers)
Closed 4 years ago.
In my current project I am taking a request parameter with a value of either array or a string. But if I get an array it would be an empty array. So what I did is I checked the type first and then I worked with the value. But then I did something like this
const reqParam = []
if (reqParam === []) {
console.log('empty array')
} else {
console.log('string')
}
But reqParam despite of being an empty array is giving me false while comparing with []. Why it is behaving like this? Thanks in advance.
Your code creates two different arrays, that's why the comparison is returning false. === will only compare their references and not their content.
This question already has answers here:
Why "foo".toString() is not the same as toString.call("foo")?
(3 answers)
Closed 7 years ago.
As the title's saying,the below two ways of using toString() are returning different results,I am wondering why , because I thought the 'this' here are the same in these two , both object arr.
var arr = [1,2,3]
toString.call(arr) //"[object Array]"
arr.toString() //"1,2,3"
toString as a "function" is actually window.toString, meaning Object.prototype.toString. On the other hand, arr.toString is Array.prototype.toString. Both methods are specialised to produce different output appropriate for their type.
This question already has answers here:
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 9 years ago.
I am aware that everything is an object in JavaScript but something struck me when i was using the console of Internet Explorer -F12 (Yes IE, not allowed to use other browsers)
If i type a sample array in the console as:
[1,2]
the output is
1,2{
0:1,
1:2
}
Does this mean that JavaScript converts Array into an Object with keys and values?
Yes, arrays in JS are simply objects with numeric keys. You could do the reverse:
var myarray = { 0: 'first', 1: 'second', 2: 'third' };
console.log(myarray[1]);