Quoting MapIterator object's next()
section,
Let entries be the List that is the value of the [[MapData]] internal slot of m.
Repeat while index is less than the total number of elements of entries. The number of elements must be redetermined each time this method is evaluated.
...
...
- Set the [[Map]] internal slot of O to undefined.
- Return CreateIterResultObject(undefined, true).
Similarly from SetIterator object's next()
section,
- Let entries be the List that is the value of the [[SetData]] internal slot of s.
- Repeat while index is less than the total number of elements of entries. The number of elements must be redetermined each time this method is evaluated.
...
...
- Set the [[IteratedSet]] internal slot of O to undefined.
- Return CreateIterResultObject(undefined, true).
Since the values are re-evaluated everytime next()
is called, if the current pointer (index) is greater than the length of the entries, then it doesn't throw an error, instead returns undefined
.
But, generally, avoid modifying the containers while iterating the same.