LoginSignup
0
0

More than 3 years have passed since last update.

デフォルト引数の一部にだけ値を渡したい場合

Posted at

JavaScriptではデフォルトの引数を指定できる。(ES2015~)

デフォルト引数を指定した引数の一部にだけ値を渡したい場合

デフォルトにしたい値には、値を渡さない or undefinedを渡せばOK。
(そもそもJavaScriptでは、関数の引数は指定しなければundefinedが渡されている。)


function multiply(a = 2, b = 1) {
  return a * b;
}

console.log(multiply(5, 2));
// expected output: 10

console.log(multiply(5));
// expected output: 5

console.log(multiply(undefined, 5));
// expected output: 10
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0