0

I was reading the JavaScript Docs I found the Array function section one Example that I could not understand properly. Because I am learning Programming.

function multiply(multiplier, ...theArgs){
    return theArgs.map(x=> multiplier*x);
}

var arr = multiply(2,1,2,3);
console.log(arr);

Above example is giving

2,4,6

My first question why is used 3 dots(...) before function argument name (...theArgs) for? And How did it calculate 2,4,6? And the second Question is about rest parameter. What does the rest parameter use in Array Function?

I would be glad if anyone can give me some real-life object example to understand this Problem

George
  • 6,116
  • 2
  • 24
  • 36
Garten786
  • 1
  • 1
  • 5
  • 2
    `...` is the [spread operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator) – George Nov 09 '17 at 14:31
  • It allows multiple arguments. As George said its called the spread operator. Examples of use can be found at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator – karen Nov 09 '17 at 14:33
  • Actually @George it is referred to as rest spread syntax https://babeljs.io/docs/plugins/transform-object-rest-spread/ it does more than just spread, depending on context of use – Robbie Milejczak Nov 09 '17 at 14:33
  • @RobbieMilejczak no, that's what it's called in babel, which is a compiler for javascript. – George Nov 09 '17 at 14:34
  • https://github.com/tc39/proposal-object-rest-spread @George and the proposal is named that because? I'm not trying to be rude, just informing you that it is not merely the spread operator – Robbie Milejczak Nov 09 '17 at 14:34

0 Answers0