# Optional Chaning(?.)

์˜ต์…”๋„ ์ฒด์ด๋‹์€ ES2020์— ์ถ”๊ฐ€๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ 3.7 ๋ฒ„์ „์—์„œ ๊ตฌํ˜„๋œ ๊ธฐ๋Šฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์…จ๋˜ ๋ถ„๋“ค์—๊ฒ ์ต์ˆ™ํ•œ ๋ฌธ๋ฒ•์ผ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ์˜ต์…”๋„ ์ฒด์ด๋‹ ๋ฌธ๋ฒ•์œผ๋กœ ๊ฐ์ฒด์˜ ์†์„ฑ ๊ฐ’์ด ์œ ํšจํ•œ์ง€ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ต์…”๋„ ์ฒด์ด๋‹(?.) ์•ž์˜ ํ‰๊ฐ€ ๋Œ€์ƒ์ด undefined๋‚˜ null์ด๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  undefined ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

# ์˜ต์…”๋„ ์ฒด์ด๋‹์ด ์ถ”๊ฐ€๋˜๊ธฐ ์ „

์˜ต์…”๋„ ์ฒด์ด๋‹ ๋ฌธ๋ฒ•์ด ์ถ”๊ฐ€๋˜๊ธฐ ์ „์— ์ค‘์ฒฉ๋œ ๊ฐ์ฒด์˜ ํ•˜์œ„ ์†์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค.

const userInfo = {
  name: {
    first: 'Hong',
    last: 'Gildong',
  },
  address: {
    city: 'Seoul',
    postcode: '04377',
  },
};

const postcode = userInfo.address && userInfo.address.postcode;

&& ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ userInfo ์— address ์†์„ฑ์ด ์žˆ๋Š”์ง€ ํ™•์ธ์„ ํ•˜๊ณ  postcode ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ์˜ต์…”๋„ ์ฒด์ด๋‹์ด ์ถ”๊ฐ€ ๋œ ํ›„

const userInfo = {
  name: {
    first: 'Hong',
    last: 'Gildong',
  },
  address: {
    city: 'Seoul',
    postcode: '04377',
  },
};

const postcode = userInfo.address?.postcode;

์˜ต์…”๋„ ์ฒด์ด๋‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ „ ์ฝ”๋“œ๋ณด๋‹ค ๋ฌธ๋ฒ•์ ์œผ๋กœ ๊น”๋”ํ•˜๊ณ  ์•ˆ์ „ํ•˜๊ฒŒ ์ค‘์ฒฉ๋œ ๊ฐ์ฒด์˜ ํ•˜์œ„ ์†์„ฑ ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const userInfo = {
  name: {
    first: 'Hong',
    last: 'Gildong',
  },
  address: {
    city: 'Seoul',
    postcode: '04377',
  },
  getInfo: () => userInfo,
};

userInfo.getInfo?.();
// userInfo object

userInfo.setInfo?.();
// undefined

์˜ต์…”๋„ ์ฒด์ด๋‹์€ ๋ฉ”์†Œ๋“œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ  ํ˜ธ์ถœํ•  ๋•Œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const userInfo = {
  name: {
    first: 'Hong',
    last: 'Gildong',
  },
  address: {
    city: 'Seoul',
    postcode: '04377',
  },
};
const key = 'city';

console.log(userInfo.address?.[key]);
// Seoul

. ๋Œ€์‹  ๋Œ€๊ด„ํ˜ธ [] ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด์˜ ์†์„ฑ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ์˜ต์…”๋„ ์ฒด์ด๋‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(??)์™€ ๊ฐ™์ด ํ™œ์šฉํ•˜๊ธฐ

์˜ต์…”๋„ ์ฒด์ด๋‹๊ณผ ๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(??)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ์†์„ฑ์˜ ๊ฐ’์˜ ์œ ๋ฌด๋ฅผ ํŒ๋ณ„ํ•˜๊ณ  ๊ธฐ๋ณธ ๊ฐ’์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const userInfo = {
  name: {
    first: 'Hong',
    last: 'Gildong',
  },
  address: {
    city: 'Seoul',
    postcode: '04377',
  },
};

const city = userInfo.address?.city ?? 'New York';

์œ„ ์ฝ”๋“œ์—์„œ city์˜ ๊ฐ’์€ ๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(??)์˜ ์™ผ์ชฝ ํ•ญ์ธ ์˜ต์…”๋„ ์ฒด์ด๋‹์œผ๋กœ ๊ฒ€์ฆํ•œ ๊ฐ์ฒด์˜ ์†์„ฑ ๊ฐ’์ด null๋˜๋Š” undefined์ด๋ฉด ๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(??)์˜ ์˜ค๋ฅธ์ชฝ ํ•ญ์ธ New York์ด ๊ธฐ๋ณธ ๊ฐ’์œผ๋กœ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.

TIP

๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(??)์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋„ ๋ณ‘ํ•ฉ ์—ฐ์‚ฐ์ž(??) ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

WARNING

์˜ต์…”๋„ ์ฒด์ด๋‹์„ ๋‚จ์šฉํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์ฃผ์„ธ์š”.

์˜ต์…”๋„ ์ฒด์ด๋‹(?.)์€ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์€ ๋Œ€์ƒ์—๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ userInfo๋Š” ๋ฐ˜๋“œ์‹œ ์กด์žฌํ•ด์•ผ ํ•˜์ง€๋งŒ address๋Š” ํ•„์ˆ˜๊ฐ’์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ userInfo.address?.city ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€๋งŒ userInfo?.address?.city ๋Š” ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด userInfo์˜ ๊ฐ’์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์„ ๋•Œ ์ฆ‰์‹œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ๋ชปํ•ด ์ถ”ํ›„ ๋””๋ฒ„๊น…์— ์–ด๋ ค์›€์„ ๊ฒช์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.