The main difference is that while for..in
iterates over the name of object properties, for..of
iterates over the values of these properties. For example:
var arr = ["gato", "cachorro", "macaco"];
for (var i in arr) {
console.log(i); // Imprime "0", "1", "2"
}
for (var i of arr) {
console.log(i); // Imprime "gato", "cachorro", "macaco"
}
Note that for arrays, for..of
is equivalent to the forEach
you can use the Array prototype:
arr.forEach(function(i) {
console.log(i); // Imprime "gato", "cachorro", "macaco"
});
Note that this feature is still experimental, and not all browsers support it - so it is not advisable to use it.
More details: link