Skip to content Skip to sidebar Skip to footer

Why Does This Javascript Array Print Out 0?

I have an array called front images written as var frontImages = [];. From what I understand javascript arrays don't need a specified length. When I run the code below, it prints 0

Solution 1:

You're printing the array to the console before the onload functions have executed, and at that time the array is empty, as the images you are waiting for has'nt loaded yet ?


Solution 2:

The images are being loaded asynchronously, so by the time console.log gets run, the length of the frontImages is still 0.

You need to wait on the images to load before you can query any information against them (either by using jQuery Deferred or some other custom construct)


Post a Comment for "Why Does This Javascript Array Print Out 0?"