Last active
September 19, 2021 11:08
-
-
Save AnsonH/1b2a031649ef874886972d7edc489af7 to your computer and use it in GitHub Desktop.
Javascript Tip #6 - Rest parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Extra links: