# ๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜(Default parameter)

๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜๋ž€ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ’์ด ์ „๋‹ฌ๋˜์ง€ ์•Š์•˜์„ ๋•Œ ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค.

# ๊ตฌ๋ฌธ

๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ฌธ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

function functionName(param1 = defaultValue1, ..., paramN = defaultValueN) {
  // ... 
}

ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์šฐ์ธก์— = ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํ•จ์ˆ˜์— ๊ฐ’์ด ์ „๋‹ฌ๋˜์ง€ ์•Š์•˜์„ ๋•Œ ์„ค์ •ํ•  ๊ธฐ๋ณธ ๊ฐ’์„ defaultValue ์œ„์น˜์— ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ๋ฒ•์„ ์ด์šฉํ•ด ์‹ค์ œ๋กœ ๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ ์šฉํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ย 






function foo(param1 = 1, param2 = {}, param3 = 'korean') {
  console.log(param1, param2, param3);
};

foo(30, { name: 'amy' }, 'american'); // 30, { name: 'amy' }, 'american'
foo(); // 1, {}, 'korean'

# ์„ค๋ช…

ES6 ์ด์ „ ๋ฌธ๋ฒ•์—์„œ๋Š” or ์—ฐ์‚ฐ์ž์ธ ||๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ๋ฐ›์€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์—ญ๋ณ€์ˆ˜์— ์žฌ์ •์˜ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. or ์—ฐ์‚ฐ์ž์˜ ํŠน์„ฑ์œผ๋กœ ์ธํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์ด falsy value(false ๋กœ ํ‰๊ฐ€๋˜๋Š” ๊ฐ’, false, null, 0, undefined...) (opens new window) ์ธ ๊ฒฝ์šฐ์— || ์—ฐ์‚ฐ์ž ์šฐํ•ญ์˜ ๊ฐ’์„ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

# || ๋ฅผ ์ด์šฉํ•œ ๊ธฐ๋ณธ๊ฐ’ ํ• ๋‹น ๋ฐฉ์‹


ย 
ย 
ย 







function printPersonInfo(height, weight, age) {
  var height = height || 180;
  var weight = weight || 60;
  var age = age || 66;

  console.log(height, weight, age);
};

printPersonInfo(10, 200, 300); // 10, 200, 300
printPersonInfo(); // 180, 100, 66

๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋”๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ  ํ• ๋‹นํ•˜๋Š” ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๊ฐ„๋‹จํ•˜๊ฒŒ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ณณ์—์„œ ๊ธฐ๋ณธ๊ฐ’์„ ๋ฏธ๋ฆฌ ์ •์˜ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜ ์‚ฌ์šฉ ๋ฐฉ์‹

ย 






function printPersonInfo(height = 180, weight = 60, age = 66) {
  console.log(height, weight, age);
};

printPersonInfo(178, 58, 29); // 178, 58, 29
printPersonInfo(100); // 100, 60, 66

# ์ฃผ์˜์‚ฌํ•ญ

ํ•˜์ง€๋งŒ ES6์˜ ๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ •์˜๋˜์ง€ ์•Š๊ณ  undefined ๊ฐ’์ธ ๊ฒฝ์šฐ์—๋งŒ ๊ธฐ๋ณธ ๊ฐ’์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ, || ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•œ ์ง€์—ญ๋ณ€์ˆ˜ ์žฌ์ •์˜ ๋ฐฉ์‹๊ณผ๋Š” ๊ทธ ๋™์ž‘์ด ๋‹ค๋ฅด๋ฏ€๋กœ, ์˜ˆ์ „ ๋ฌธ๋ฒ•์— ์ต์ˆ™ํ•œ ๊ฒฝ์šฐ ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์•„๋ž˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋กœ ์ข€ ๋” ์ƒ์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

// || ๋ฅผ ์ด์šฉํ•œ ๊ธฐ๋ณธ๊ฐ’ ๋ฐฉ์‹
function printFruit(name, weight, price) {
  var name = name || 'apple';
  var weight = weight || 10;
  var price = price || 15000;

  console.log(name, weight, price);
};

printFruit(0, false, null); // apple, 10, 15000


// ๊ธฐ๋ณธ๊ฐ’ ๋งค๊ฐœ๋ณ€์ˆ˜ ์‚ฌ์šฉ ๋ฐฉ์‹
function printFruit(name = 'apple', weight = 10, price = 15000) {
  console.log(name, weight, price);
};

// 0, false, null์„ ๊ฐ’์œผ๋กœ ์ธ์‹ํ•˜์—ฌ ๊ธฐ๋ณธ๊ฐ’ ๋Œ€์ฒด๋˜์ง€ ์•Š์Œ
printFruit(0, false, null); // 0, false, null
// ๊ฐ’์ด ์—†๊ฑฐ๋‚˜ undefined ์ผ ๋•Œ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋Œ€์ฒด
printFruit(); // apple, 10, 15000