I read the explanations of for...in and for...of but then faced this situation where in the same case they behave differently.
//case1
function avg(...args) {
var sum = 0;
for (let value of args) {
sum += value;
}
return sum / args.length;
}
console.log(avg(2,3,4,5));
//case2
const people = function(param1, param2, ...rest) {
console.log(param1);
console.log(param2);
for(let i in rest) {
console.log(rest[i]);
}
}
people('Foo', 'Bar', 'Catz', 'Dogz');
So, the ...args
& ...rest
are arrays (object) right? I think the case1 is correct and behave as it explained in many sources (e.g. MDN for...of)
What is their difference, why in case2 for...in works instead of for...of ?