Select Page
JavaScript ES6+ : Spread & Rest Operator
Jayman Pandya
May 3, 2020

Spread takes all the items in the specified iterables (arrays, objects etc) and copies it.

const indoorActivities = ['Table Tennis', 'Carrom', 'Chess',  'Gym'];
const outdoorActivities = ['Swimming Pool', 'Badminton Court', 'Tennis Court'];

const clubFacilites = [...indoorActivities, 'Squash Court', ...outdoorActivities];

console.log(`indoorActivities : ${indoorActivities}`);
console.log(`outdoorActivities : ${outdoorActivities}`);

console.log(`clubFacilites : ${clubFacilites}`);

When you do not know the number of arguments that will be passed in a function you can use Rest operator.

function calculateFines(baseFee, countyFee, tax = 0.18, ...fines) {
    console.log(baseFee, countyFee, tax = 0.18, fines);
    return fines.map(fine => baseFee + countyFee + tax + fine);
}

const totalFines = calculateFines(400, 0, .05, 24, 100, 76);

console.log(totalFines);

Peace ✌️

Let's Talk

If you liked what you have seen, then let’s connect and make something amazing together.

Email me at converse@jaymanpandya.com or send a DM on my twitter @jaymanpandya