Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 230
ᅙᄉᅙ Avatar asked Sep 11 '15 14:09

ᅙᄉᅙ


1 Answers

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.

like image 61
Daniel A. White Avatar answered Nov 10 '22 18:11

Daniel A. White