python์์๋ ๋ณ์๋ฅผ ์ ์ธ๋ง ํ๊ณ ๊ฐ์ ์ ๋ฃ๋ ๊ฒ ๋ถ๊ฐ๋ฅํ๋ฏ๋ก(NameError), undefined ๊ฐ๋ ์ด ์์
๋ณ์ ์ ์ธ์ ๋ฌด์กฐ๊ป ์ด๊ธฐํ๋ฅผ ๋์์ ์์ผ์ค์ผํจ ( a = [] )

๋ณ์๋ฅผ ์ ์ธ๋งํ๊ณ ๊ฐ์ ๋ฃ์ด์ฃผ์ง ์์ผ๋ฉด undefined.
null, none์ ๋ฐ์ดํฐ๊ฐ ์๋ค๋๊ฒ์ด ๋ฐ์ดํฐ
TS์์ null๊ณผ undefined์ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ
null ๊ฐ์ด ๋ค์ด์ฌ ๊ฐ๋ฅ์ฑ์ด ์๋ ๊ฒฝ์ฐ ์ ๋์จํ์ ์ผ๋ก ๋ณ์ ์ง์
let address: string | null = "Seoul";
1. ์ต์ ๋ ์ฒด์ด๋ (?.)
// user๊ฐ null์ด๋ฉด, user.address์ ์ ๊ทผํ ๋ ์๋ฌ๊ฐ ํฐ์ง(JS์ ๊ณ ์ง๋ณ)
// TS ํด๊ฒฐ์ฑ
:
const city = user?.address?.city;
// ํด์: user๊ฐ ์์ด? -> ์์ผ๋ฉด address๊ฐ ์์ด? -> ์์ผ๋ฉด city๋ฅผ ์ค.
// ์์ผ๋ฉด? -> ๊ทธ๋ฅ undefined๋ฅผ ์ค (์๋ฌ X)
๋ฐ์ดํฐ๊ฐ ์๋์ง ์๋์ง๋ฅผ ํ์ธ -> ?
๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด? -> undefined ๋ฐํ
2. ๋ณํฉ ์ฐ์ฐ์ - Nullish Coalescing Operator (??)
๊ฐ์ด ์์๋ (null, undefined์ธ ๊ฒฝ์ฐ) default๊ฐ ์ง์
// price๊ฐ null์ด๋ undefined๋ฉด 0์ ๋ฃ์ด๋ผ.
const finalPrice = product.price ?? 0;
* JS/TS์ || ์ฐ์ฐ์๋ ์ซ์ 0์ด๋ ๋น ๋ฌธ์์ด ""๋ '๊ฑฐ์ง'์ผ๋ก ์ทจ๊ธํ์ง๋ง, ??๋ ์ค์ง null/undefined์ผ ๋๋ง ์๋
* or์ฐ์ฐ์์ ๋ณํฉ์ฐ์ฐ์ >> default value๊ฐ์ ์ง์ ํ ๋๋ ?? ์ ์ฌ์ฉํ์.
// ์์: ๋๋ค์ ์ค์
const inputName = ""; // ์ฌ์ฉ์๊ฐ ์ด๋ฆ์ ์
๋ ฅ ์ ํ๊ณ ๊ทธ๋ฅ ์ํฐ ์นจ (๋น ๋ฌธ์์ด)
// 1. || ์ฌ์ฉ ์
const name1 = inputName || "์ต๋ช
";
console.log(name1); // "์ต๋ช
" (๋น ๋ฌธ์์ด๋ ๊ฑฐ์ง ์ทจ๊ธํด์ ๋ฎ์ด์)
// 2. ?? ์ฌ์ฉ ์
const name2 = inputName ?? "์ต๋ช
";
console.log(name2); // "" (์ฌ์ฉ์๊ฐ ๋น ์ด๋ฆ์ ์ํ๋ค๊ณ ์กด์คํด์ค)
'Develop Study ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [TypeScript] ํ์ ์คํฌ๋ฆฝํธ๋? (0) | 2026.01.18 |
|---|---|
| [TypeScript] ์ ๋์จ ํ์ , ์ธํฐ์น์ ํ์ / | & / ์ ๋์จํ์ ๊ณผ or์ฐ์ฐ ์ฐจ์ด/ | || (0) | 2026.01.18 |
| ์ ์ ํ์ , ๋์ ํ์ / ์ปดํ์ผ ํ์, ๋ฐํ์ (0) | 2026.01.18 |
| Computation Architect (0) | 2026.01.16 |
| [github] ๋ก์ปฌ, main๊ณผ ๊ผฌ์์ ๋ / ํ์ผ ์๊ฒน์น๋๋ฐ merge์๋ ๋ (0) | 2026.01.12 |