Skip to content Skip to sidebar Skip to footer

Chrome Developer Tool Array Length

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 Ar

Solution 1:

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.


Post a Comment for "Chrome Developer Tool Array Length"