ES6 Banner image Welcome to the 6th post in the ES6 101 series and the topic for today is the Rest operator. The rest operator now allows us to process infinite parameters in a function.😯 Yes. really! If you remember the spread operator ... we discussed previously in this series, the rest operator looks the same ... Let’s dig into how we can use the rest operator. It is really simple and easy to work with.

Examples for ES6 Rest operator

Problem Statement: Calculate the sum of all numbers sent to the add function. To achieve this in ES5, we would find the number of parameters and then run a for loop that many number of times and calculate the sum. In ES6, with the rest operator it is a cake walk.

    let sum = (a, ...b) => {
        console.log(a); // This is the first parameter.
        console.log(b); // This is an array of the remaining parameters.
        console.log("Total sum is: " + (a + b.reduce((num, total) => num + total)));
    }
    sum(1,2,3,4,5); // 15
    sum(1,2,3); // 6
    sum(7,8,9); // 24

As you can see, it is pretty imple and straight forward to use the rest operator. 😍 We no longer need to loop through the parameters of the function, find their length and all that hassel! So what are you waiting for? Go ahead and start modifying your code right away! Will see you in the next post tomorrow…