0

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 ?

Aaron
  • 349
  • 5
  • 18
  • No. `...args` is similar to `fn.apply(context, argsList)` – Rajesh May 23 '17 at 12:54
  • @Rajesh—if by "context" you mean [*thisArg*](https://tc39.github.io/ecma262/#sec-function.prototype.apply). – RobG May 23 '17 at 12:55
  • Yes, [*Arrays are Objects*](http://ecma-international.org/ecma-262/7.0/index.html#sec-array-objects) (which is why ECMA-262 calls them "Array Objects"). Is that your question? for..in and for..of behave differently because they are specified to behave differently. One iterates over enumerable property names, the other over values of properties. – RobG May 23 '17 at 12:57
  • `for of` iterates over the values `for in` iterates over the keys. So in case2 you'll get `0 + 1 + 2` etc. Case1 you'll get `2 + 3 + 4` etc. – Dan Gamble May 23 '17 at 12:59
  • @RobG No, I know that, I think in both cases as they are similar, for...of should be the right choice but as you can see in case2 for...in works correctly. Why? Are my cas1 & case2 different? – Aaron May 23 '17 at 13:01
  • 2
    @Aaron — You've got two functions, that you are feeding different input, and processing that input in different ways. You describe one of the results as "correct" and don't describe the other at all. I have absolutely no idea what you *expect* to happen in either case, not why you think the behaviour of one is "correct". – Quentin May 23 '17 at 13:02
  • 2
    How are these cases *the same*? They get different arguments and do entirely different things. – trincot May 23 '17 at 13:02
  • @RobG Yes. Please correct me if I'm wrong. – Rajesh May 23 '17 at 13:03
  • 2
    Sorry, struggling to see a question. *for..of* didn't exist until ECMAScript 2015, before then *for..in* was it. *for..of* just cuts out the step of accessing values via the key (and doesn't go up the `[[Prototype]]` chain like *for..in* does). – RobG May 23 '17 at 13:04
  • @Quentin See, by saying they are the same I mean the function which take any number of arguments are the same. So, the case1 is doing its job correctly by using for-of, but I did the same thing in case2 (because the ...rest is the thing as ...args) and it returned undefined. – Aaron May 23 '17 at 13:08
  • @trincot by the same I mean ...args & ...rest are the same thing, not what functions do. – Aaron May 23 '17 at 13:10
  • @Aaron — So your question is "Why does the people function return undefined?"? – Quentin May 23 '17 at 13:10
  • Maybe add `return` statement? What is your desired output? We can keep guessing as to what you want, but it would help if you specify the desired behaviour. Because as it stands, I see nothing surprising happening. All is OK. – trincot May 23 '17 at 13:10
  • @Quentin No, Why people function works fine with for...in? Isn't it the same structure as the case1's ...args? – Aaron May 23 '17 at 13:12
  • @Aaron — Define "works fine". I still haven't got a clue what you think the problem is. – Quentin May 23 '17 at 13:17
  • @Quentin Well I just got what I did. In case2 I'm using the index itself `rest[i]` while in case1 I'm accessing the value itself. Thanks everyone. – Aaron May 23 '17 at 13:17

0 Answers0