Searching for Signal

the n01se blog

They really tried, but….

I'm sure they really tried, but they just plain screwed it up.

for( var i = 0; i < foo.length; ++i ) { ... }

That pattern is used very often in JavaScript to do something with each item in the array foo, setting aside for the moment that it's often not quite correct (if the var i is declared anywhere else in the same function).

You'd think they would have provided a more succinct way to walk
through an array. And actually they tried:

for( var i in foo ) { ... }

but they screwed it up, because in this case i is set not to each
value in foo, but to each index, 0 through whatever. ...as a string.

So the first time through, i is the string "0". Fortunately (I suppose) this still works as a parameter to your array, because you can say foo["0"] to get the initial item in the array.

But the knowledge of the amount of work being done to convert the integer range 0 through length into a series of strings, back to ints, and then look them up again in the array just pains me too much. It's not that the performance has actually ever hurt me in such cases, it's just that I can't stand to do it.

So I type for( var i = 0; i < foo.length; ++i ) one more time, and glance around to make sure i isn't being used for anything else nearby...