0

enter image description here

Why chrome developer tool showing 0 array length (first line Array[0]) even if there are total 9 objects in it?

At first line of image should be like Array[9] why it is showing Array[0]

enter image description here

In second image array has 13 objects so it is showing Array[13] not Array[0]

Dhruv
  • 173
  • 11

1 Answers1

2

It seems like you are logging the output of the array before you are adding objects to your array. Something like

var arr = [];
console.log(arr);
arr.push({}); //pushing blank obj

Here, executing this code will result in Array[0] but it does hold your object and will return a length of 1 if you try arr.length.

This might also happen if you are having some sort of Async function which pushes item to your array, which will result in the same thing. For example,

var a = [];
setTimeout(function(){
  a.push({});
  a.push({});
}, 1000);
console.log(a);

I think, this behavior is intentional, where Chrome wants to show that initially, your Array was empty, later on, items were pushed on runtime.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278