JavaScript “For …in” with Arrays

array2-799x400

Iteration using for…

var a = [];
a[5] = 5; // Perfectly legal Javascript that resizes array
for (var i=0; i<a.length; i++) {
      // Iterates over numeric indexes from 0 to 5, as everyone expects
}

Iteration using for in …

var x, a = []; a[5] = 5; 
for (x in a) {     
 // Shows only the explicitly set index of "5", and ignores 0-4 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s