🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #191

Re: Using for ... in with arrays

#191
From: Bob Stucky
Date: Tue, 31 Mar 2009 16:24
18 lines
1295 bytes
There is a difference between the two. In the second example below, i is a string, not an int. (put a debugger statement in there and do typeof( i ); in the console to prove it). In the second example we're asking for the array index of:
"01", "11", "12"...

And they're all undefined.

I NEVER use the for( var i in array ) syntax for that reason. If you do introduce a bug there, it's one of those bugs that your eye doesn't catch, and takes forever to find.

Bob

var array = [0,1,2,3,4,5,6,7,8,9];

for ( var i = 0; i < array.length - 1; i++ ) {
	$.writeln( array[ i + 1 ] );
}

for ( var i in array ) {
	$.writeln( array[ i + 1 ] );
}
Technical Details
Message-ID: <59b87616.2@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!news2.glorb.com!newsfeed.news2me.com!newshub.sdsu.edu!webcrossing
References: <59b87616.-1@webcrossing.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,709551d954315a95
X-Google-Attributes: gidf939a,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Bob Stucky" <rstucky@starband.net>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!news2.glorb.com!newsfeed.news2me.com!newshub.sdsu.edu!webcrossing
Newsgroups: adobe.scripting.indesign
Subject: Re: Using for ... in with arrays
Message-ID: <59b87616.2@webcrossing.la2eafNXanI>
Date: Tue, 31 Mar 2009 09:24:14 -0700
References: <59b87616.-1@webcrossing.la2eafNXanI>
Lines: 23
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit