There is a
for each (variable in list) statement in Gecko 1.8a2. It is meant for E4X compatibility but seems to be available for all types of lists. Maybe this should be documented? --Wladimir Palant 08:00, 21 Jul 2005 (PDT)
ECMA 262 says nothing about the order of
for .. in's enumerated attributes being lexicographically sorted/ordered, but all versions of JS appear to do this sorting. I've seen code that relies on it. Worth a documentation note?
note on use with arrays
In my opinion, the Description covers the warning expressed in the added text.
It does, but using for...in with arrays, and expecting it to return purely numeric indexes, is a common newbie mistake, and in my opinion warranted the more verbose note. So I'm restoring the note removed in the 11 February 2006 revision. --Tmetro 10:40, 20 September 2006 (PDT)
mutating the array
It might be worth a note that mutating the array (using Array.splice in my case) would exhibit the same behaviour one would see with a manual for loop - some elements get skipped / repeated / etc. Not going to do that myself with the recent posts in m.d.mdc about accidentally changing meaning though! :) --Mook 01:10, 24 December 2006 (PST)
- _Please_ make the change yourself. You can try getting a review from waldo or someone afterwards. If you don't make it, there's a high chance that no-one will. --Nickolay 07:35, 24 December 2006 (PST)
- I just made some changes which should hopefully address the mutation aspect. Feel free to say something here if you see improvements to my wording; I'm sure there are several which could be made, but I was trying not to be overly verbose. (Regarding hesitancy to make changes, I echo Nickolay -- make the change and post a request for review to the newsgroup, please. :-) ) --Waldo 19:46, 6 January 2007 (PST)
'never use on arrays'
for...in statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a
"foo" property to it or even by adding a method or property to
for...in statement will return the name of your user-defined properties in addition to the numeric indexes.