Files
freeCodeCamp/guide/english/javascript/rest-parameters/index.md
Deanna Tran 338d7ee8a7 Change "rest operator" to say "rest parameter" in challenges and guide (#35496)
* change es6 challenges rest operator to param

* fix rest parameter typos

* change rest operator to parameter in guide

* fix casing

* change rest operator to rest parameter in guide

* change rest operator to rest parameter in curriculum

* remove extra whitespace

* remove whitespaces

* remove whitespace

* fix: removed arabic file

* fix: removed chinese file
2019-05-08 16:30:24 +02:00

1.3 KiB

title
title
Rest Parameters

Rest Parameters

In ES6, the rest parameter syntax ... allows you to gather up an indefinite number of arguments into an array.

Even though they look the same, it does the opposite of the spread operator, which takes every item from an iterable and spreads them out into their individual values.

Syntax

function myFunc(...args) {
  console.log(args);
}

myFunc( 1, 2, 3, 4, 5);       // [1,2,3,4,5]

You can prefix a function's last parameter with ... when you want to do something with the initial parameters and then treat all of the remaining parameters differently.

function convertCurrency(rate, fee, ...amounts) {
  return amounts.map(amount => (amount * rate) + fee);
}

convertCurrency(0.89, 2.5, 100, 250, 75, 150, 300); // [ 91.5, 225, 69.25, 136, 269.5 ]

The ... lets you gather up the rest of the arguments, if there are any, into an array.

The difference between rest parameters and the arguments object

arguments is an array-like object, available inside functions, that contains the arguments passed to those functions. It's called "array-like" because it doesn't have all of an array's built in methods, such as .forEach() and .map().

The rest parameters are an array, with all of the array methods included.