1

I have a loop like the following:

for (var i = 0; i < arr.length; i++) {
    console.log(arr);
    // rest of the code does not touch 'arr'
    ...
}

However, in the browser console (Chrome), I keep getting this:

Array[0]
    length: 0
    __proto__: Array[0]

as the output of console.log(arr), which suggests that there is nothing in arr. But then how is it possible that the logging statement gets executed?!

6
  • at the end of your script (all the script) the array is empty? Commented Oct 18, 2013 at 13:56
  • I tried with an empty array and it doesn't enter the loop. Commented Oct 18, 2013 at 13:56
  • 1
    What's in arr then? Is it empty or not? Commented Oct 18, 2013 at 13:57
  • JavaScript works, if i is zero and arr.length is zero then i < arr.length is false and it will not enter your loop. So arr is not of zero length. Commented Oct 18, 2013 at 14:00
  • Where do initiate arr and with what method ? Commented Oct 18, 2013 at 14:01

2 Answers 2

1

Probably, the array is empty at the end of the script, because chrome console show you the current state of the object (does change when the object itself change) when it have been opened after script execution. (I think it's an issue)

To reproduce this:

var arr = [1];
for (var i = 0; i < arr.length; i++) {
    console.log(arr);
}

arr.pop(); // empty the array

http://jsfiddle.net/47nrc/ (only for chrome)

execute it with console closed, then when you open it you could see Array[0]

One solution could be to log a string representation of the array:

console.log(arr.join());

See also: https://code.google.com/p/chromium/issues/detail?id=50316

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

3 Comments

this sounds likely. I ran the script with the console closed, and I can confirm that the loop was entered a number of times during a (long-running) execution (evidenced by the a series of output of console.log(arr) with the corresponding line number). Each of this output is empty.
and you pop all elements of the array as well?
eventually (after the script stops), yes, which is when I inspect the console for logging messages.
0

See this fiddle: http://jsfiddle.net/r9exf/

var arr = [];

for (var i = 0; i < arr.length; i++) {
    console.log("arr = " + arr);
    // rest of the code does not touch 'arr'

}

compared to:

var arr2 = [1,2,3];

for (var i = 0; i < arr2.length; i++) {
    console.log("arr2 = " + arr2);
    // rest of the code does not touch 'arr'

}

Your code (assuming what you have provided is what you have), is just working fine. It does not enter the loop (see the first case and compare with second in the fiddle).

So, it must be somewhere else in your code which is logging this array.

1 Comment

thanks for your suggestion. But in Chrome console, each logging output corresponds to a line number, and I can confirm that the shown line number is exactly the one for the console.log(arr) inside that for loop. So it must be something else than what you've suggested.

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.