Use the Rest Operator with Function Parameters--- Can anyone tell me how this code output in 6.my doubt is they are passing 3 values but given => a+b .how this possible

Tell us what’s happening:
Can anyone tell me how this code output in 6.my doubt is they are passing 3 values but given => a+b .how this possible

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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-rest-operator-with-function-parameters

Add logs to see what is happening.

const sum = (function() {
  "use strict";
  return function sum(...args) {
    console.log(args);
    return args.reduce((a, b) => a + b, 0);
  };
})();

sum(1, 2, 3);  // logs [1, 2, 3]  | returns 6

args is an array and each element of args is an argument that was passed into the function.
The methods .reduce will convert every element of an array into a single value based on the rules specified by the callback function.

Bro can you explain it clearly. I understood till this console.log(args); …can you please explain thisreturn args.reduce((a, b) => a + b, 0); because they used a,b for three values

The full explanation is in the link that @EdMagal provided. After you read it, if you still have questions. Tag or reply to me with any areas that are still fuzzy and I’ll happily explain further.

@Rajakumar040391

args.reduce((a, b) => a + b, 0);

1st loop: a=0, b=1 adds them (0+1=1)
2nd loop: a=1, b=2 adds them (1+2=3)
3rd loop: a=3, b=3 adds them (3+3=6)
Returns 6 because it looped through 1,2,3

At least, I think that’s what’s happening. :slight_smile:

ya i understand it clearly bro camper…initialValue should be optional right.

@Rajakumar040391 Yes, in this case.

Thank you so much bro

What is array.prototype…Can anyone please explain it clearly

Every object can have methods added to an associated object called a prototype, and those get shared between all objects of the same type. So Array.prototype methods are methods available to all arrays. Otherwise you would have to define every method for every object, like length or reduce for every array you ever wanted to check the length of or apply reduce to

1 Like

A prototype is a specification for every value of a given type. Array is a built-in type in JavaScript and every array you create is defined by Array.prototype. That means every JavaScript Array you create will automatically expose all the properties and methods of Array.prototype.

You can see the list of properties and and methods for a JavaScript Array here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype

Notice that length is a property defined on Array.prototype; that means every Array you create will automatically expose a length property. This is why the following code works:

['a','b','c'].length // 3

You see length is defined in the prototype so it’s basically “built-in” and just works. Same thing for a method like reduce, it’s defined on the prototype so you can just use it on any Array.

Some methods are defined on the type but not on the prototype, like of and isArray. For example:

Array.of('a','b') // works
[].of('a','b') // throws an error

Array.isArray(['a','b']) // works
[].isArray(['a','b']) // throws an error

Hope that helps

1 Like

I am not understanding it (Prototype) clearly. can anyone please explain it with examples

@Rajakumar040391, can you be more specific about what part you’re not understanding? I think @DanCouper and @BillSourour explained it pretty well, using examples. I think it’ll help us help you if you quote the info you have questions about from what’s been shared already. Does that make sense?

Anything that says {someObject}.prototype.{someFunction} in the documentation means there is a function you can use on any of that type of object (any array or any string or any Object etc), and it affects the object you apply it to.

So Array.prototype.reduce is a function called reduce that you can use on any array, and you use it like myArray.reduce. In that case reduce is applied to myArray.

[1,2,3].reduce(//some code)
"hello".toUpperCase()
etc.


Anything that says {someObject}.{someFunction} in the documentation is just a standalone function, it doesn’t get applied to a calling object like the prototype methods, you use it like a normal function:

Array.isArray(myArray)
String.fromCharCode(1)
etc