i-think Twenty-Two

Now with more coherency.

Javascript for (x in y) is stupid

| Comments

That’s right, the for (x in y) used by JavaScript is dumb. And what is more, they can’t change it without royally screwing up people who have worked around it.

When used with arrays, the x will represent the index of the array, not the value (as is common in most languages).

Sure, I agree that this is a useful feature to have, but why must it do it by default.

So to recap: To iterate through an array in Javascript using this control structure you must do it as shown below:

forloop.js
1
2
3
for (key in SomeArray) {
  theValue = SomeArray[key];
}

Stupid.

Comments