0

I was reviewing some alternative solutions in a codewars kata and came across this line in the 'top' solution:

result.push(...algo(numbers[i]));

What is the purpose of the '...' in this context? I haven't seen this before and can't find any reference to it anywhere.

Is this a new feature in ES6?

If you need more context it was the 'Split all even numbers to odd ones in different ways' kata, and was the solution by the user 'ph0en1x'.

Don't want to post the whole code here for fear of spoiling the challenge for any other potential users.

Thanks if anyone is able to help clarify!

Michael Martinez
  • 171
  • 1
  • 3
  • 10
  • 3
    It's [*spread syntax*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator), there has to be a dupetarget... – T.J. Crowder Jan 26 '17 at 08:55

1 Answers1

4

It is an ES6 feature, called spread operator.

It's mean is that you have an array object like this [1,2,3] and have a function that takes in 3 parameters, you don't need to pass like func(arr[0], arr[1], arr[2]). You can just spread func(...arr) the array and it will do the same instead of you.

var arr = [1,2,3];

function func(a,b,c){
  console.log(a); // arr[0]
  console.log(b); // arr[1]
  console.log(c); // arr[2]
}

func(...arr);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112