2
var array = [];
var arr = [];
var i;
for (i = 0; i < 2; i++) {
    var temp = [];// using array[i] = new Array() works fine
    array.push(temp);
    arr.push(temp);
}
array[0].push(1);
arr[0].push(2);
alert(array[0]);

The above javascript code gives [1,2] as out put. Where as using 'new Array()' instead of 'push([])' gives [1]. I was able to find the issue, but I don't get reason why. can some one explain this

3
  • First you have to explain what you want to perform. Commented Jul 13, 2013 at 10:11
  • You write what you get, but could you also tell us what you expected to get as output? Commented Jul 13, 2013 at 10:15
  • 1
    array[0] and arr[0] refer to the same array. Commented Jul 13, 2013 at 10:48

2 Answers 2

2

arrays work like pointers to some location in the memory so the arr variable dosent store the actual values that u are pushing in fact it is pointing to a memory location where the values are stored and in this case it points to "temp" so when you write

for (i = 0; i < 2; i++) {
  var temp = [];// using array[i] = new Array() works fine
  array.push(temp);
  arr.push(temp);
}

what is happening is that in each loop its creating a new temp but both of "array an arr" are pointing to the same location in the memory so when one of them is modefing the contents of this location its changing the data that the other one is pointing to

meanwhile if use the following code

var array = [];
var arr = [];
var i;
for (i = 0; i < 2; i++) {
  array[i] = new Array();
  arr[i] = new Array();

}
array[0].push(1);
arr[0].push(2);
alert(array[0])

what will hapen here is that each time the "new Array()" is called it will allocate a new variable in the memory and pass-back its location to the array pointer that you created "array & arr" so now each one of the previos pointers is pointing to a different place in the memory

Sign up to request clarification or add additional context in comments.

Comments

1

This code will create one array and put reference to it inside both arrray and arr

var temp = [];// using array[i] = new Array() works fine
array.push(temp);
arr.push(temp);

So when you run:

array[0].push(1);
arr[0].push(2);
alert(array[0]);

Then, you first push 1 to original tmp, that has reference in array[0], and then push 2 again to tmp, which is too in arr[0], so you'll get [1,2] as a result.

I didn't see your full alternative code but i assume if you have this in your for loop:

array[i] = new Array();
arr[i] = new Array();

Then, you'll have two separate arrays in array and arr, so when you run again:

array[0].push(1);
arr[0].push(2);
alert(array[0]);
alert(arr[0]);

You'll get 1 in array[0] and 2 in array[1].

1 Comment

thanx. I didn't know it'll put the reference to temp in the arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.