P Varga
P Varga

Reputation: 20249

Why does for...in fail gracefully but for...of throw an Exception?

Why is it that

for (let e in null) void e

fails gracefully, but that

for (let e of null) void e

throws a TypeError? Doesn't this result in an inconsistency?

Upvotes: 8

Views: 96

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190956

for...of only works on iterable objects (i.e. objects implementing iterable protocol) which null is not either of these. Whereas for...in works on all values.

Upvotes: 6

Related Questions