ES6 - Use the Rest Parameter with Function Parameters

Can somone explain to me why " total += args[i]; " because in this solution wouldn’t i = 2 ? So I don’t understand why total += args would be args[2] if used const sum = (1,2,3) in place of …args and the for loop “for (let i = 0; i < args.length; i++)”

Your code so far

const sum = (...args) => {
  let total = 0;
  for (let i = 0; i < args.length; i++) {
    total += args[i];
  }
  return total;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.60

Challenge: ES6 - Use the Rest Parameter with Function Parameters

Link to the challenge:

i is the counting variable you are using to go through the args array. So it starts at 0 and then increments by 1 each time you go through the for loop.

Don’t let the fact that you are using a rest parameter to create an array confuse you. The args array is just like any other array, and you are just using a simple for loop to go through it and add all the numbers in it together.