When you initialize an array like var myArray = new Array(3);
, it creates an empty array. Therefore, even if myArray
and otherArray
had the same values - three undefined
values - they would still be considered different arrays. This is because an array is treated as an object, and a variable like myArray
merely holds a reference to that object. Having two objects with identical values does not make them equal.
For example,
var x = new Object();
var y = new Object();
console.log(x===y); // Outputs false.
Additionally:
var person1 = { name: "John" };
var person2 = { name: "John" };
console.log(person1===person2); // Outputs false.
Note:
Also, in the case of initializing an array using var myArray = new Array(3)
, the indices are not initialized, as noted by Paul in the comments.
For instance, if you do this:
var nums = [1,2,3];
console.log(Object.keys(nums));
You will see:
["1","2","3"];
However, if you try:
var nums = new Array(3);
console.log(Object.keys(nums));
You will get an output of:
[]