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!
Top comments (0)