Spread & Rest

New ES6 Features to Me

Thu, 24 Jan 2019

Unsplash

While working through the ES6 course by Wes Bos we were shown two new features that I think are really powerful and extremely useful - spread and rest. They are also like two sides of a coin so going over them together makes the most sense.

Spread operator will take anything that is an iterable like a string or an array and expand it. Say I had two arrays and I wanted to join them while adding an item in the middle. Here’s how you would do it before and now with spread:

const nbaWest = ["Warriors", "Lakers", "Jazz"];
const nbaEast = ["Raptors", "Celtics", "Bucks"];

// old school
let oldNba = [];
oldNba = oldNba.concat(nbaWest);
oldNba.push("Spurs");
oldNba = oldNabe.concat(nbaEast);

// Using rest operator
const newNba = [...nbaWest, "Spurs", ...nbaEast];

Rest parameters on the other hand will take a list of arguments and combine them together into an array. On example of where you might want to use this is if you have a function where you might have an infinite number of arguments but the first few are static. A concrete example of this would be a tip calculator where you get the tip amount broken up for each item but the first two arguments are the tax rate and the tip percentage. Here’s how that would look:

// Function example
function tipCalc(taxRate, tipPercent, ...items) {
  return items.map(item => item * tipPercent * taxRate);
}

// Example of using the function
const subTotals = tipCalc(0.07, 20, 12.5, 10.99, 24.78);
SHARE
André Wanlin

André Wanlin is a Full Stack Developer and Team Leader at Petline Insurance Company, where André leads application development, including system administration and support. André has worked in .NET since 2008 and is passionate about DevOps and development methodologies like Lean, Agile, Scrum and Kanban. He loves to talk about Azure DevOps (formerly Team Foundation Server). André is a dog owner, an avid concert goer, and traveler from Winnipeg, Manitoba. You can reach him at andre@wanlin.ca or go to andre.wanlin.ca or you can catch him walking his dog at one of the many dog parks in the city.