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

**URL:** <https://es.discourse.group/t/deprecate-for-loops-in-favor-of-for-in-and-for-in-reverse/1107>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 26, 2021, 9:14am UTC](https://es.discourse.group/t/deprecate-for-loops-in-favor-of-for-in-and-for-in-reverse/1107 "2021-11-26T09:14:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seagat2011](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seagat2011/32/367_2.png) [@Seagat2011](https://es.discourse.group/u/Seagat2011)\
**Post date:** [November 26, 2021, 9:14am UTC](https://es.discourse.group/t/deprecate-for-loops-in-favor-of-for-in-and-for-in-reverse/1107/1 "2021-11-26T09:14:28Z")

</div>

```javascript
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 //
}

```

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [November 26, 2021, 9:47am UTC](https://es.discourse.group/t/deprecate-for-loops-in-favor-of-for-in-and-for-in-reverse/1107/2 "2021-11-26T09:47:39Z")

</div>

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.

> [@Seagat2011](#):
>
> ```javascript
> // NEW (FORWARD)
> for(let i in obj){
> 
> ```

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())`.

> [@Seagat2011](#):
>
> ```javascript
> // NEW (REVERSE)
> for(let i in reverse obj){
> 
> ```

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())`

> [@Seagat2011](#):
>
> ```javascript
> // NEW (Filter)
> for(let i in Filter(obj, return_prime_only_indices_callback)){
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![rdking](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rdking/32/54_2.png) [@rdking](https://es.discourse.group/u/rdking)\
**Post date:** [November 26, 2021, 2:27pm UTC](https://es.discourse.group/t/deprecate-for-loops-in-favor-of-for-in-and-for-in-reverse/1107/3 "2021-11-26T14:27:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DiriectorDoc](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/diriectordoc/32/987_2.png) [@DiriectorDoc](https://es.discourse.group/u/DiriectorDoc)\
**Post date:** [November 26, 2021, 9:55pm UTC](https://es.discourse.group/t/deprecate-for-loops-in-favor-of-for-in-and-for-in-reverse/1107/5 "2021-11-26T21:55:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [November 27, 2021, 8:42am UTC](https://es.discourse.group/t/deprecate-for-loops-in-favor-of-for-in-and-for-in-reverse/1107/6 "2021-11-27T08:42:40Z")

</div>

You might be interested in

> **[GitHub - tc39/proposal-Number.range: A proposal for ECMAScript to add a...](https://github.com/tc39/proposal-Number.range)**
>
> A proposal for ECMAScript to add a built-in Number.range() - GitHub - tc39/proposal-Number.range: A proposal for ECMAScript to add a built-in Number.range()

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [November 28, 2021, 3:06pm UTC](https://es.discourse.group/t/deprecate-for-loops-in-favor-of-for-in-and-for-in-reverse/1107/7 "2021-11-28T15:06:35Z")

</div>

Also as far as reverse goes, there's:

> **[GitHub - tc39/proposal-deiter: Double-Ended Iterator and Destructuring](https://github.com/tc39/proposal-deiter)**
>
> Double-Ended Iterator and Destructuring. Contribute to tc39/proposal-deiter development by creating an account on GitHub.
