Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active January 20, 2018 12:13
Show Gist options
  • Save kevinchisholm/93a4006ca7bfa5ddb2dd3e9ecec72e91 to your computer and use it in GitHub Desktop.
Save kevinchisholm/93a4006ca7bfa5ddb2dd3e9ecec72e91 to your computer and use it in GitHub Desktop.
JavaScript Rest Parameter – Basics
x -> a
arg 0 -> b
arg 1 -> c
arg 2 -> d
arg 3 -> e
arg 4 -> f
function inspectArgs(x, ...theArgs) {
if (x) {
console.log('x -> ' + x);
}
theArgs.forEach((arg, index) => {
console.log('arg ' + index + ' -> ' + arg);
})
}
inspectArgs('a', 'b', 'c', 'd', 'e', 'f');
x -> a
arg 0 -> c
arg 1 -> d
arg 2 -> e
arg 3 -> f
function inspectArgs(x, y, ...theArgs) {
if (x) {
console.log('x -> ' + x);
}
theArgs.forEach((arg, index) => {
console.log('arg ' + index + ' -> ' + arg);
})
}
inspectArgs('a', 'b', 'c', 'd', 'e', 'f');
arg 0 -> a
arg 1 -> b
arg 2 -> c
arg 3 -> d
arg 4 -> e
arg 5 -> f
function inspectArgs(...theArgs) {
theArgs.forEach((arg, index) => {
console.log('arg ' + index + ' -> ' + arg);
})
}
inspectArgs('a', 'b', 'c', 'd', 'e', 'f');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment