[Easy] ๐ null & undefined
๋ ๊ฐ์ง ์ฐจ์ด์ ๋น๊ตโ
undefined๏ผ- ๋ณ์๊ฐ ์ ์ธ๋์์ง๋ง ์์ง ๊ฐ์ด ํ ๋น๋์ง ์์์์ ๋ํ๋ ๋๋ค.
- ์ด๊ธฐํ๋์ง ์์ ๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ ๋๋ค.
- ํจ์๊ฐ ๋ช
์์ ์ผ๋ก ๊ฐ์ ๋ฐํํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก
undefined๋ฅผ ๋ฐํํฉ๋๋ค.
null๏ผ- ๋น ๊ฐ ๋๋ ๊ฐ์ด ์์์ ๋ํ๋ ๋๋ค.
- ์ผ๋ฐ์ ์ผ๋ก ๋ช
์์ ์ผ๋ก
null์ ํ ๋นํด์ผ ํฉ๋๋ค. - ๋ณ์๊ฐ ์๋์ ์ผ๋ก ์ด๋ค ๊ฐ์ฒด๋ ๊ฐ๋ ๊ฐ๋ฆฌํค์ง ์์์ ๋ํ๋ด๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
์์โ
let x;
console.log(x); // ์ถ๋ ฅ: undefined
function foo() {}
console.log(foo()); // ์ถ๋ ฅ: undefined
let y = null;
console.log(y); // ์ถ๋ ฅ: null
let obj = { x: 5 };
obj.x = null;
console.log(obj.x); // ์ถ๋ ฅ: null
typeof๋ฅผ ์ด์ฉํ ๊ฒ์ฆโ
console.log(typeof undefined); // ์ถ๋ ฅ: "undefined"
console.log(typeof null); // ์ถ๋ ฅ: "object"
console.log(null == undefined); // ์ถ๋ ฅ: true
console.log(null === undefined); // ์ถ๋ ฅ: false