Deprecate for-loops in favor of for-in and for-in-reverse!

Although for-loops have traditionally offered superior performance, 
the for-loop is a tried-and-true source of errors

As an unstructured solution, I propose the following 

// ORIGINAL
for(let i=0; i<MAX; i++){
    obj[i]
}

// NEW (FORWARD)
for(let i in obj){
    // code //
}

// NEW (REVERSE)
for(let i in reverse obj){
    // code //
}

// NEW (Filter)
for(let i in Filter(obj, return_prime_only_indices_callback)){
    // code //
}

No. C-style for loops serve more purposes than iterating whole arrays.

Besides, what you're proposing is both dangerous and useless. Dangerous because for...in should never be used to iterate over an array. Useless because when I'm iterating over an array, I rarely need indices, but I almost always need values.

This is already valid syntax, and it enumerates string keys in obj and its prototype, not integer indices of array. If you want only indices without values, use for (let i of arr.keys()). If you want indices and values, use for (let [i, v] of arr.entries()).

There is existing syntax to achieve what you mean (assuming you really want to go out of your way to ditch performance): for (let i of Array.from(arr.keys()).reverse())

for (let i of arr.filter(return_prime_only_indices_callback).keys())
for (let v of arr.filter(return_prime_only_indices_callback))

2 Likes

I'd also add that the notion of for loops being a "tried and true source of errors" is about as fallacious as it gets. The source of errors are the developers who in lieu of learning the nuances of the language, lean heavily on the language to constrain things they didn't learn properly. If you think developers are making this kind of mistake often with for loops, try writing an article somewhere detailing these common mistakes so that future developers can read and learn to avoid those kinds of mistakes.

1 Like

I would only be in favour of this if and only if JS also natively introduces a Python-like range(x) function, which creates an array from 0 to x. And even then, I don't think it would be a very good idea.

Also, it would be practically impossible to switch now. Every single website ever would have to be recoded, which would be a nightmare.

You might be interested in

Also as far as reverse goes, there's: