DEV Community

Cover image for Rest Parameters and Spread Operators in Javascript
Lori "Lei" Boyd
Lori "Lei" Boyd

Posted on

1 2

Rest Parameters and Spread Operators in Javascript

Javascript has a functionality where depending on where it's used, ... can either be a rest parameter or a spread operator.

Spread Operators

spread operators allow an iterable object like an array to be expanded, unpacking each element from the array.

let fourToSix = ['four', 'five', 'six']
let numbers = ['one', 'two', 'three', ...fourToSix ]

=> ["one", "two", "three", "four", "five", "six"]

Rest Parameters

A rest parameter will bundle up all remaining elements in a parameter into an array.

const greetings = (friend, ...everyoneElse ) => {
  console.log(`Hello, ${friend}!`)
  everyoneElse.forEach(person => console.log(`Nice to meet you, ${person}!`))
}

greetings('Lei', 'Cierra', 'Aiden', 'Pierre', 'Miranda')
=> Hello, Lei!
=> Nice to meet you, Cierra!
=> Nice to meet you, Aiden!
=> Nice to meet you, Pierre!
=> Nice to meet you, Miranda!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE