for..of vs. for..in in TypeScript
Both for..of
and for..in
statements iterate over lists; the values iterated on are different though.
for..of
returns a list of values of the numeric properties of the object being iterated.
for..in
returns a list of keys on the object being iterated.
Let’s see an example that demonstrates this distinction:
let list = ["Tech", "Xposer", "Article"]; for (let i of list) { console.log(i); // "Tech", "Xposer", "Article" } for (let i in list) { console.log(i); // "0", "1", "2" }
Note: When targeting an ES5 or ES3, iterators (i.e., for..of & for..in ) are only allowed on values of Array
type.
Happy Coding… 🙂