Skip to content

Instantly share code, notes, and snippets.

@AnsonH
Last active September 19, 2021 11:08
Show Gist options
  • Save AnsonH/1b2a031649ef874886972d7edc489af7 to your computer and use it in GitHub Desktop.
Save AnsonH/1b2a031649ef874886972d7edc489af7 to your computer and use it in GitHub Desktop.
Javascript Tip #6 - Rest parameters
/* Tweet: https://twitter.com/AnsonH_/status/1439546876718551049?s=20 */
/* First photo */
function myFunc(a, b, ...args) {
// Remaining arguments are stored in `args` array
console.log("a: ", a);
console.log("b: ", b);
console.log("args: ", args); // This is an array!
}
myFunc(1, 2, 3, 4, 5, 6);
// a: 1
// b: 2
// args: [ 3, 4, 5, 6 ]
/* Second photo */
function foo(arg1, arg2, ...restArgs) {} // Correct
function foo(...args); // Correct
function foo(arg1, ...restArgs, arg2) {} // WRONG!
function foo(arg1, ...restArgs, ...moreArgs) {} // WRONG!
@AnsonH
Copy link
Author

AnsonH commented Sep 19, 2021

Extra links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment