ํฐ์คํ ๋ฆฌ ๋ทฐ
๋ ผ๋ฆฌ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์กฐ๊ฑด๋ฌธ์ ์ฒ๋ฆฌํ ๋
์ ์ฒด ํํ์์ ํ๊ฐํ์ง ์๊ณ ์ต์ํ์ ํ๊ฐ๋ก ๊ฒฐ๊ณผ๋ฅผ ๋์ถํ๋ ๋ฐฉ์์ ๋งํ๋ค.
๋จ์ถ ํ๊ฐ๋ ์ฃผ๋ก && (๋ ผ๋ฆฌ๊ณฑ), || (๋ ผ๋ฆฌํฉ), ?? (null ๋ณํฉ ์ฐ์ฐ์)๋ฅผ ์ฌ์ฉํ ๋ ๋ฐ์ํ๋ค.
๋ ผ๋ฆฌํฉ( || ) ์ฐ์ฐ์
๋ ผ๋ฆฌํฉ ์ฐ์ฐ์ ||๋ ์ข๋ณ์ ํผ์ฐ์ฐ์๊ฐ falsy ๊ฐ(false, 0, "", null, undefined, NaN)์ผ ๋๋ง ์ฐ๋ณ์ ํผ์ฐ์ฐ์๋ฅผ ํ๊ฐํ๋ค.
์ข๋ณ์ ํผ์ฐ์ฐ์๊ฐ truthy ๊ฐ์ผ ๊ฒฝ์ฐ, ๊ทธ ๊ฐ์ด ๋ฐ๋ก ๊ฒฐ๊ณผ๊ฐ์ผ๋ก ๋ฐํ๋๋ฉฐ, ์ฐ๋ณ์ ํ๊ฐ๋์ง ์๋๋ค.
์ฝ๊ฒ ์ค๋ช ํ์๋ฉด
A || B
A๊ฐ false๋ผ๋ฉด B๋ฅผ ๋ฐํํ๊ณ ,
A๊ฐ true๋ผ๋ฉด B๋ ๋ฌด์, A์ ๋ง๋ ๊ฐ์ ๋ฐํํ๋ค.
+) falsyํ ๊ฐ : false, 0, "", null, undefined
๐์์
// ์ ์ ์ ๋ณด๊ฐ ์ ๊ณต๋์ง ์์์ ๋ ๊ธฐ๋ณธ ์ด๋ฆ ์ ๊ณต
function getUsername(user) {
return user.name || '์ ์๋ฏธ์';
}
console.log(getUsername({ name: '๋ฅดํ์ด' })); //๋ฅดํ์ด
console.log(getUsername({})); //์ ์๋ฏธ์
๐๐ป์ด ์์ด ์ด๋ป๊ฒ ๋์จ ๊ฑฐ์ง?
๊ณผ์
1. getUserName ์ ๊ฐ์ ๋ฐํํ๊ธฐ
function getUsername(user) {
return user.name;
}
const person = {
age: 25,
};
console.log(getUsername(person);
=> undefined์ด ๋์จ๋ค. (falsyํ ๊ฐ)
2. ๊ฐ์ด undefined ์ผ ๋ "์ ์ ๋ฏธ์"์ ์ถ๋ ฅํ๊ธฐ
function getUsername(user) {
if(user.name === undefined) {
return "์ ์ ๋ฏธ์"
}
return user.name;
}
const person = {
age: 25,
};
console.log(getUsername(person);
3. ๊ฐ์ด falsy ํ ๋๋ก ํ์ฅํ๊ธฐ
function getUsername(user) {
if(!user.name) {
return "์ ์ ๋ฏธ์"
}
return user.name;
}
const person = {
age: 25,
};
console.log(getUsername(person);
4. ๊ฐ๋จํ ๋ํ๋ด๊ธฐ
function getUsername(user) {
return user.name || "์ ์ ๋ฏธ์"
}
const person = {
age: 25,
};
console.log(getUsername(person);
5. ํ์ดํ ํจ์ ์ ์ฉํ๊ธฐ
const = getUsername = (user) => return user.name || "์ ์ ๋ฏธ์"
const person = {
age: 25,
};
console.log(getUsername(person);
๋ ผ๋ฆฌ๊ณฑ(&&) ์ฐ์ฐ์
๋ ผ๋ฆฌ๊ณฑ ์ฐ์ฐ์ &&๋ ์ข๋ณ์ด truthy์ผ ๋๋ง ์ฐ๋ณ์ ํ๊ฐํ๋ค.
์ฝ๊ฒ ์ค๋ช ํ์๋ฉด
A || B
A๊ฐ true์ผ ๋๋ง B๋ฅผ ๋ฐํํ๋ค.
์กฐ๊ฑด์ ๋ฐ๋ผ ํน์ ์ฝ๋๋ฅผ ์คํํ๊ณ ์ ํ ๋ ์ ์ฉํ๋ค.
๐์์
// ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธ ์ํ์ด๋ฉด ํ์ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ
let loggedIn = true;
let username = '๋ฅดํ์ด';
loggedIn && console.log('ํ์ํฉ๋๋ค! ' + username); //ํ์ํฉ๋๋ค! ๋ฅดํ์ด
loggedIn = false;
loggedIn && console.log('ํ์ํฉ๋๋ค! ' + username); //์๋ฌด๊ฒ๋ ์ถ๋ ฅ๋์ง ์์
Optional Chaining (?.)
๊ฐ์ฒด์ ์์ฑ์ ์ ๊ทผํ ๋ ํด๋น ๊ฒฝ๋ก์ ์์ฑ์ด ์กด์ฌํ์ง ์์๋ ์๋ฌ๋ฅผ ๋ฐ์์ํค์ง ์๊ณ , ๋์ undefined๋ฅผ ๋ฐํํ๋ค.
์ค๋ฅ๋ฅผ ๋ฐ์์ํค์ง ์๊ธฐ ๋๋ฌธ์, ๊ฐ์ฒด์ ์ค์ฒฉ๋ ์์ฑ์ ์์ ํ๊ฒ ์ ๊ทผํ ์ ์๋ค.
๐์๋ ๋ฐฉ์
์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ ๊ฐ์ฒด๊ฐ ์๋ค๊ณ ๊ฐ์ ํ์:
const user = {
profile: {
name: "๋ฅดํ์ด",
details: {
age: 30,
location: "์์ธ์ ๊ฐ๋จ๊ตฌ"
}
}
};
์ฌ๊ธฐ์ ์ฌ์ฉ์์ ๋์ด๋ฅผ ์ ๊ทผํ๋ ค๋ฉด ์ผ๋ฐ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์๋ค:
console.log(user.profile.details.age); // ์ถ๋ ฅ: 30
๊ทธ๋ฌ๋, profile ๋๋details๊ฐ undefined ๋๋ null์ผ ๊ฒฝ์ฐ ์ด ์ฝ๋๋ ์๋ฌ๋ฅผ ๋ฐ์์ํจ๋ค.
Optional chaining์ ์ฌ์ฉํ๋ฉด, ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ์๋ฐฉํ ์ ์๋ค:
console.log(user.profile?.details?.age); // ์ถ๋ ฅ: 30
์ด์ profile์ด๋ details ์ค ํ๋๋ผ๋ undefined๋ null์ด๋ฉด,
?. ์ฐ์ฐ์๋ ๊ทธ ์์ ์์ ํ๊ฐ๋ฅผ ๋ฉ์ถ๊ณ undefined๋ฅผ ๋ฐํํ๋ค:
const user = {};
console.log(user.profile?.details?.age); // ์ถ๋ ฅ: undefined
Optional chaining์ ๊ฐ์ฒด์ ์์ฑ๋ฟ๋ง ์๋๋ผ, ๋ฉ์๋ ํธ์ถ์๋ ์ฌ์ฉํ ์ ์๋ค.
์๋ฅผ ๋ค์ด, ๊ฐ์ฒด๊ฐ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ ๊ฒฝ์ฐ์๋ ์์ ํ๊ฒ ๋ฉ์๋๋ฅผ ํธ์ถํ ์ ์๋ค:
const result = user.profile?.getDetails?.();
// user.profile.getDetails๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด undefined ๋ฐํ
optional chaining์ ์ค์ฒฉ๋ ๊ฐ์ฒด ๊ตฌ์กฐ์์ ์์ ํ๊ฒ ๊ฐ์ ์ฝ๊ฑฐ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋๋ฉฐ,
์ฝ๋์ ์์ ์ฑ์ ๋์ด๊ณ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋๋ ๋ฐ ๋์์ ์ค๋ค.
Null ๋ณํฉ ์ฐ์ฐ์ (??)
?? ์ฐ์ฐ์๋ ์ข๋ณ์ด null์ด๋ undefined์ผ ๊ฒฝ์ฐ์๋ง ์ฐ๋ณ์ ํ๊ฐํ๋ค.
null ๋๋ undefined๊ฐ ์๋ falsy ๊ฐ๋ค์ ์ ํจํ ๊ฐ์ผ๋ก ์ฒ๋ฆฌํ๊ณ ์ถ์ ๋ ์ฌ์ฉ๋๋ค.
// ์ฌ์ฉ์์ ์์น ์ค์ ์ด ์์ผ๋ฉด ๊ธฐ๋ณธ ์์น๋ฅผ ์ ๊ณต
let userLocation = null;
console.log(userLocation ?? 'Unknown location'); // ์ถ๋ ฅ: Unknown location
userLocation = 'Seoul';
console.log(userLocation ?? 'Unknown location'); // ์ถ๋ ฅ: Seoul
// ์ฌ์ฉ์ ์
๋ ฅ์ด 0์ธ ๊ฒฝ์ฐ์๋ 0์ ์ ํจํ ๊ฐ์ผ๋ก ์ทจ๊ธ
const temperature = 0;
console.log(temperature ?? 25); // ์ถ๋ ฅ: 0
๐||(๋ ผ๋ฆฌ OR) ์ ??(null ๋ณํฉ ์ฐ์ฐ์)์ ์ฐจ์ด!
truthyํ ๊ฐ์ด๋ ์๋๋ vs null์ด๋ undefined์ด๋ ์๋๋
function displayPreferences(preferences) {
// `||` ์ฐ์ฐ์ ์ฌ์ฉ ์
const textLength = preferences.textLength || 50; // textLength๊ฐ 0์ผ ๊ฒฝ์ฐ 50์ด ํ ๋น๋จ
console.log(`Text Length: ${textLength}`);
// `??` ์ฐ์ฐ์ ์ฌ์ฉ ์
const itemsPerPage = preferences.itemsPerPage ?? 10; // itemsPerPage๊ฐ null ๋๋ undefined์ผ ๋๋ง 10์ด ํ ๋น๋จ
console.log(`Items Per Page: ${itemsPerPage}`);
}
// ํ
์คํธ ์ผ์ด์ค
const userPreferences = {
textLength: 0, // 0์ด ์ ํจํ ๊ฐ์ด์ง๋ง || ์ฐ์ฐ์๋ก ์ธํด 50์ด ์ถ๋ ฅ๋จ
itemsPerPage: null // null ๊ฐ์ ๋ํด ?? ์ฐ์ฐ์๋ก ์ธํด 10์ด ์ถ๋ ฅ๋จ
};
displayPreferences(userPreferences);
'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ! includes('@') (0) | 2025.01.24 |
|---|---|
| typeof๋ฅผ ํตํด ์๋ฃํ์ ํ์ธํด์ผ ํ๋ ์ด์ (0) | 2025.01.24 |
| [๐ฆ] ํ์ดํ ํจ์ (Arrow function) (0) | 2025.01.20 |
| [๐ฆ] JS ๋ฌธ๋ฒ - Rest operator (0) | 2025.01.20 |
| JavaScript ๋ฌธ๋ฒ ์ข ํฉ 3์ฃผ์ฐจ ๊ณผ์ (1) | 2025.01.19 |