This var myArray = new Array(3);
will create an empty array. Hence, for this reason, myArray
and otherArray
are different arrays. Furthermore, even if they had the same values, three undefined
values, the arrays wouldn't be the same. An array is an object and the variable myArray
holds a reference to that object. Two objects with the same values aren't the same.
For instance,
var a = new Object();
var b = new Object();
console.log(a===b); // outputs false.
In addition to this:
var customerA = { name: "firstName" };
var customerB = { name: "firstName" };
console.log(customerA===customerB); // outputs false.
Update
Furthermore, in the case of var myArray = new Array(3)
even the indices aren't initialized, as correctly Paul pointed out in his comment.
If you try this:
var array = [1,2,3];
console.log(Object.keys(array));
you will get as an output:
["1","2","3"];
While if you try this:
var array = new Array(3);
console.log(Object.keys(array));
you will get as output:
[]