Use the Rest Operator with Function Parameters2

Tell us what’s happening:

Your code so far

const sum = (function() {
  "use strict";
  return function sum(...args) {
    return args.reduce((a, b) => a + b, 0);
console.log(sum(1, 2, 3)); // 6

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:

I must admit I got this answer on this forum. The rest op part of it makes sense to me but I don’t understand where the spread operator comes in. The spreading happens in the reduce method? Why did the instructions say “sum function uses the spread operator…”? I took that to mean we had to explicitly put the spread operator in our code somewhere. What am I missing? Thanks!

The spread operator, or better when used in the parameters, the rest operator, is the three dots ...

1 Like

Try to console.log(args) to see what it represents inside the function sum.

When you use (…args) you are stating that the function accepts any number of variables, which are concatenated in the array args :slight_smile:

1 Like