If I’m not mistaken, it’s because comparing an array or object with another array or object is always false. This is for reasons not immediately apparent, but I’ve read it’s because arrays can have private data inside.
For instance you can have an array of functions with variables inside the functions. Just like having a function inside of a function (as you may have learned already) the outer function does not have access to the inner functions variables. So an array can have information that cant be accessed by the comparison operator, so it can’t declare it true
Disclaimer: I’m reaaaaallly stretching the limits of my understanding on the above point, so anyone more knowledgeable please correct me if I’m wrong.
Factoid: The following can cause unintended results and is probably frowned upon by most devs (along with using == rather than === in general). You can compare an array to a string, and javascript (if using == not ===) will coerce the array into one string with each item separated by commas, to compare to the other string.
Factoid-Disclaimer: You probably shouldnt do this
eg
arr=[];
console.log(arr=="") //true
or
arr2=[1,2,3];
console.log(arr=="1,2,3") //true
Heres an example of just one problem with this (there are probably many potential problems with this)
function yada(){
console.log("yadayada")
}
arr2=[yada,"2"];
console.log(arr2=="yada,2") //false
This is called “type coercion” and its a source of contention in Javascript and one of the reasons many people advise always using === over ==
To keep it straightforward (not necessarily simple) stick to triple equals === which is strict equality - avoid double equals ==
Now [] === [] is false because there are two distinct empty arrays being compared and strict equality on objects compares the references to the objects not the contents of the objects - similarly [2, 4, 6] === [2, 4, 6] is false
Now what if you have a=[2, 4, 6] and b=[2,4,6] and want to check if a and b have the same values in the same order? You have to go through both arrays comparing values at each index - this is not too bad for numbers though I’d reconsider the approach in my solution before doing this - it gets difficult if the array elements are themselves objects and have to be deep-compared
maybe… if there’s a way to fully “expand” the array (along with its object elements) and parse that thing to a str? (like sometimes you see a bunch of hard-to-read code when you accidently print some object or other things) then you could simply compare 2 strs
In JS there are two types of values: primitive values (number, string, boolean, null, undefined and symbol), and one type of reference value (objects).
When you assign a primitive value to a variable, that variable refers directly to that single value in the computer’s memory, and when you manipulate it you manipulate it directly. You can compare them because two of the same values are literally the same.
But reference values are a collection of properties. In essence, if you assign an object to a variable, you aren’t referring to a concrete “thing”, you’re setting up a data structure that may contain some primitive values:
var a = 1;
var b = [];
var c = [];
// b does not == c, they're two different things
b.push(a);
c.push(a);
// b and c still have exactly the same structure,
// and both contain the same exact value, but
// they're still not the same