/ code

How to use the ... operator (spread) in ES6 JavaScript

Most of the new ES6 JavaScript specification have been implemented in the latest versions of the popular browsers. It offers a lot of new syntax, including new ways to define functions. The arrow operator allows you to define functions but also changes how implicit parameters like this and arguments are defined.

If you want to define a function that takes a variable amount of parameters, ES6 offers the ...args syntax now (the spread operator). For example, if you wanted to create a string formatting function similar to the ones found in C# or Python you could implement it like this:

let format = (s, ...args) => {
 return s.replace(/{(\d+)}/g, (match, number) => {
   return typeof args[number] != 'undefined' ? args[number] : match;

The variable s will always be the first argument past in. Any subsequent variables will be past into the args variable. This can be accessed much in the same way arguments would in a standard JavaScript function.