Optional Chaining

const a = {}
console.log(a.b); // undefined

const c = null;
// try {
//     c.d;
// } catch (e) {
//     console.error(e); // TypeError: Cannot read properties of null (reading 'd')
// }

// 예전에는 아래와 같이 if문으로 안전장치를 넣어줬었다.
// if (c) {
//     c.d;
// }

// 하지만 위와 같이 작성하는건 너무 귀찮다.
// 때문에 옵셔널 체이닝이 나왔다.
console.log(c?.d); // undefined
// c가 없으면 없는대로, 있으면 그 안에서 d를 읽는다.
console.log(c?.f()); // undefined

console.log(a?.b?.c?.d?.e); // undefined
// 그렇다고 위와 같이 남용하는건 좀..
// 타입스크립트를 사용하면 ?를 붙여야될 때와 안 붙여도될 때를 확실하게 알려준다.

console.log(c?.[0]); // undefined