develwoutacause’s avatardevelwoutacause’s Twitter Archive—№ 1,836

  1. Today's #WTF #JavaScript snippet. The following code loops infinitely. Apparently the Set iterator dynamically updates with new data added to the Set. This applies *even* when you explicitly call .values() before iterating.
    A JavaScript code snippet which reads:

```
const set = new Set([1]);

for (const value of set.values()) {
  console.log(value);
  set.delete(value);
  set.add(value + 1);
}
```
    1. …in reply to @develwoutacause
      You have to do Array.from(set.values()) to fully snapshot the state first, before you can safely iterate over it with mutations.