# Better for loops with values and keys

**URL:** <https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830>\
**Category:** 💡 Ideas\
**Created:** [October 23, 2023, 7:16am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830 "2023-10-23T07:16:41Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 25, 2023, 5:23am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/21 "2023-10-25T05:23:21Z")

</div>

1. I said it was barely a new syntax. That means it is new syntax, but barely.

2. Sure, it's a consideration and I just considered that it's really easy.

---

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 25, 2023, 5:24am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/22 "2023-10-25T05:24:24Z")

</div>

And why are you being so angry at me for something I didn't say?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 25, 2023, 5:36am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/23 "2023-10-25T05:36:23Z")

</div>

> And also this is barely a new syntax. It's a comma and then another of a thing that is already there, not a brand new syntax, so your analogy is disingenuous

Barely a new syntax is brand new syntax, so the analogy is correct.

---

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 25, 2023, 5:56am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/24 "2023-10-25T05:56:00Z")

</div>

Can you not read? I'm suggesting an addition to an existing syntax, you're suggesting a whole new syntax. If I say we should add headlights to a car, it's like you asking why we haven't made a motorbike. FFS, can't believe I have to explain this.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 25, 2023, 6:19am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/25 "2023-10-25T06:19:06Z")

</div>

"Can you not read?" violates our Code of Conduct, so be forewarned.

I'm saying that _any modification to syntax whatsoever_ is "new syntax", whether it's a new construct or a change to existing syntax.

In your analogy, literally any hardware change is "new syntax".

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [October 25, 2023, 9:11am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/26 "2023-10-25T09:11:40Z")

</div>

> [@coolCucumber-cat](#):
>
> This is an extremely common problem

I disagree. I rarely if ever have to iterate over key-value pairs of objects in modern JS. When something is a keyed collection with a dynamic number of entries, where a need for iteration might arise, it's a `Map` not an object. The syntax for iterating `Map`s is just fine and straightforward.

---

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 25, 2023, 11:38am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/27 "2023-10-25T11:38:56Z")

</div>

So because you don't do it, it's not common? That doesn't make sense. Who said it had to be a dynamic collection? Or even an plain object at all, like an array. Because if you use forEach, you can't break or continue and return just does what continue does. If you want an early return or a break you can't do that without some wacky stuff.

---

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 25, 2023, 11:40am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/28 "2023-10-25T11:40:08Z")

</div>

There's a difference between modifying some syntax by adding a small feature and adding entirely new syntax.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [October 25, 2023, 2:23pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/29 "2023-10-25T14:23:17Z")

</div>

@coolCucumber-cat - I understand that the addition of this new syntax is somewhat minor - just a comma. That is, indeed a pro for it and one of the things I already weighed in when I said I personally don't feel like it's worth adding this syntax.

But, don't feel like you have to convince me otherwise - I'm just a random person on the Internet. There's some active syntax proposals tc39 has that I don't think should go in, and other syntax proposals that I really want - I share my opinions on these proposals, but that's really all I can do.

---

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 25, 2023, 2:29pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/30 "2023-10-25T14:29:24Z")

</div>

I still don't know what you think the downsides are, other than the fact that there's a syntax change. It's just a comma. If you needed to break out of a forEach loop, how would you do it? And the fact that you no longer have to decide which loop you want to use or everyneed to worry about this is nice too and we wouldn't be here wasting more time. You're not even the one that has to implement it or use it so I'm not sure why you would even care.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [October 25, 2023, 2:59pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/31 "2023-10-25T14:59:34Z")

</div>

Let me approach it this way.

The JavaScript language already has way too many ways to do a loop, on of which (for each) isn't even syntactic. You also have the c-style for loop with the loop variable, and for in, and for of. While this syntax change you're proposing is "just a comma", it's also "yet another way to do a loop", it just happens to be living inside the for of loop's syntax. But, as you said, it doesn't use the iterator protocol so semantically it's very different from today's for of loop, even though you're able to fit the syntax into it.

If we're going to add a new way to loop (weather or not new syntax is involved), we better have a really, really good reason for it. Not liking to type out "Object.entries()" isn't strong enough of a reason for me personally.

---

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 25, 2023, 3:21pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/32 "2023-10-25T15:21:51Z")

</div>

When did I say it doesn't use the iterator protocol? It works the exact same way as Symbol.iterator, just with keys and values, not just values. I never said anything about the iterator protocol, other than when you mention it and I ask what the hell the it has to do with anything and that I never said anything about the iterator protocol. So once again, what the hell does this have to do with the iterator protocol?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 25, 2023, 3:54pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/33 "2023-10-25T15:54:48Z")

</div>

for..of uses the iterator protocol by invoking a Symbol.iterator method on the iteratee. Objects don't generically have one.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [October 25, 2023, 4:09pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/34 "2023-10-25T16:09:12Z")

</div>

> [@coolCucumber-cat](#):
>
> What does this have to do with the iterator protocol or array methods?

I summarized this quote as "it doesn't use the iterator protocol". If it has nothing to do with the iterator protocol, I would assume that means it doesn't use it. Indeed, I don't know how it would use it.

---

<div class="post-metadata">

**Author:** ![jamesalancobban](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@jamesalancobban](https://es.discourse.group/u/jamesalancobban)\
**Post date:** [October 26, 2023, 11:43pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/35 "2023-10-26T23:43:11Z")

</div>

For me the aspect of the traditional:

> [@coolCucumber-cat](#):
>
> ```javascript
> for (const key in object) {
> const value = object[key] 
> }
> 
> ```

That I find disconcerting is that it is difficult to optimize. The for loop must iterate over the attributes of the object, but it extracts only the key. The statement inside the loop then uses the key to extract the value of the attribute. I wonder if it would be possible to consider:

```javascript
for (const key : const value n object) {
 ...
}

```

which follows the initialization syntax of objects.

---

<div class="post-metadata">

**Author:** ![tabatkins](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tabatkins/32/170_2.png) [@tabatkins](https://es.discourse.group/u/tabatkins)\
**Post date:** [October 27, 2023, 5:55pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/36 "2023-10-27T17:55:12Z")

</div>

Sorry, I'm not familiar with who you are - are you an implementer who is noticing a difficult-to-optimize hot path? Or are you speculating?

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [October 28, 2023, 11:04am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/37 "2023-10-28T11:04:37Z")

</div>

If it's not a dynamic collection, I wouldn't need to loop over it, I'd know which keys it has - so that's uncommon.  
If it's not a plain object but an array, there's already `.entries()`. (I personally never use `.forEach` for precisely the reason you mention).

And sorry for implying that what I do defines the common case. It doesn't. But I want to say that the problem (common or not) already has a straightforward solution in the language: the use of a `Map` instead of an object. I practice this solution, and have no troubles with it.

---

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 28, 2023, 11:39am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/38 "2023-10-28T11:39:49Z")

</div>

Exactly. Then why mention it? Just because I ask what that has to do with anything, doesn't mean it's not true. If you asked me why I breath air and I ask what that has to do with looping over an object, doesn't mean I don't breath.

---

<div class="post-metadata">

**Author:** ![coolCucumber-cat](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/coolcucumber-cat/32/1892_2.png) [@coolCucumber-cat](https://es.discourse.group/u/coolCucumber-cat)\
**Post date:** [October 28, 2023, 11:46am UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/39 "2023-10-28T11:46:06Z")

</div>

Just because it's not dynamic, doesn't mean you don't need to know the keys. It could have optional keys, there might be loads of different variations. Just because it's not dynamic, doesn't mean it has to be completely static. And I can't always use a map. Using a map means I have to be the only one doing something with it, if I want to pass it around, everything else better also use maps.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [October 28, 2023, 4:47pm UTC](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830/40 "2023-10-28T16:47:08Z")

</div>

Sorry if I misquoted you then.

But, is the assumption wrong? Does it use the iterator protocol? From the way you described the feature in the o.p. it sounds like the answer is no, instead it uses a new protocol that's similar to the iterator protocol, but also different.

So I believe my original conclusion still stands.

[Previous page](https://es.discourse.group/t/better-for-loops-with-values-and-keys/1830.md?page=1)
