Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created Jun 30, 2015
Embed
What would you like to do?
//Rest Parameters
rest(1,2,3);
function rest(...args){
console.log(args); // [1, 2, 3]
console.log(arguments); // [1, 2, 3]
console.log(Array.isArray(args)); //true
console.log(Array.isArray(arguments)) //false
args.forEach(x => console.log(x)); //1 2 3
arguments.forEach(x => console.log(x)); //Error
}
function rest(x, y, ...others) {
console.log(x, y, others);
}
rest(1, 2, 3); // 1 2 [3]
//引数が渡らなかった場合は空の配列になる
rest(1); //1 undefined []
//最後の引数以外には使用できない
function rest(first, ...arr, last) { } //NG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment