foreach

With arrays being an important part of JavaScript, it's always been frustrating that iterating one requires a for loop. Would it be possible to add a foreach construct which would essentially transform into a for loop? or into an ES5 function call?
depending on compiler option.

@Tharaxis: Why can type inference not handle this case? Currently, type inference makes strings out of all elements, but I think this looks like a bug.

@EisenbergEffect: Why it's not a recommended good practice? I'd rather have one construct to enumerate over collection-like objects. As Tharaxis said, the compiler is able to choose the more efficient implementation.

I think the reason for that is likely the way for-in works vs. a standard for loop. With arrays in JS, doing a for-in will return all the values but also any additional properties you have added to that array, so for example:

will produce the output 1, 2, 3, "foo" instead of just 1, 2, 3. Since the value of a for-in array can return any combination of whatever values + strings that exist on that object, type inference will probably always break down in
some way, hence my recommendation that arrays be treated differently (and monotyped preferably since JS arrays can also be [1, 2, "foo", "bar", 4] and so on).

EDIT:

Actually, thinking about it now, I suspect the dynamic behaviour of arrays (any indexed value can have any type) is the primary reason it can't really work.

There is no direct way of knowing what exact value you're accessing within the for-in closure (is the v in x the first element which is a number or the 10th element which is a string?) however, accessing via index (such as in a standard for-loop) gives
the compiler/parser a direct line to what value you're trying to retrieve and can therefore light up with valid type information.

It's nice to know that a polyfill is included when using an ES5 method and targeting an ES3 browser. However, I'd still like to have a dedicated foreach that is compiled into a for loop since that is more performant than the forEach method. A lot of the
code I'm writing these days is related to game programming or at least graphics/canvas rendering, so loops are common and performance is important.