study-note

2. 関数

目次


関数宣言

function 関数名(仮引数1, 仮引数2) {
  //処理
  return 関数の返り値;
}
//関数呼び出し
const 関数の結果 = 関数名(引数1, 引数2)
console.log(関数の結果)    //関数の返り値

関数の引数

引数の個数

デフォルト引数

function 関数名(仮引数1 = デフォルト値1, 仮引数2 = デフォルト値2){

}

可変長引数

残余引数

function fn(...args) {
  console.log(args);
}
fn("a", "b", "c");    //["a", "b", "c"]

Spread構文

arguments

function fn() {
  console.log(arguments[0]);
  console.log(arguments[1]);
  console.log(arguments[2])
}
fn("a", "b", "c");


関数の引数と分割代入