[Easy] ๐ JavaScript Operators
1. What is the difference between == and === ?โ
==์===์ ์ฐจ์ด์ ์ ๋ฌด์์ธ๊ฐ์?
๋ ๋ค ๋น๊ต ์ฐ์ฐ์์ด์ง๋ง, ==๋ ๋ ๊ฐ์ด ๊ฐ์์ง ๋น๊ตํ๊ณ , ===๋ ๋ ๊ฐ์ด ๊ฐ๊ณ ํ์
๋ ๋์ผํ์ง ๋น๊ตํฉ๋๋ค. ๋ฐ๋ผ์ ํ์๋ ์๊ฒฉ ๋ชจ๋๋ผ๊ณ ๋ณผ ์ ์์ต๋๋ค.
์ ์๋ JavaScript์ ์ค๊ณ์ ์๋์ผ๋ก ํ์ ๋ณํ์ ์ํํ๊ธฐ ๋๋ฌธ์, ์ง๊ด์ ๋ฐํ๋ ๊ฒฐ๊ณผ๊ฐ ๋ง์ด ๋ฐ์ํฉ๋๋ค. ์๋ฅผ ๋ค์ด:
1 == '1'; // true
1 == [1]; // true
1 == true; // true
0 == ''; // true
0 == '0'; // true
0 == false; // true
์ด๋ ๊ฐ๋ฐ์์๊ฒ ํฐ ์ธ์ง์ ๋ถ๋ด์ด ๋๋ฏ๋ก, == ๋์ ===๋ฅผ ์ฌ์ฉํ์ฌ ์๊ธฐ์น ์์ ๋ฒ๊ทธ๋ฅผ ๋ฐฉ์งํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ผ๋ก ๊ถ์ฅ๋ฉ๋๋ค.
๋ชจ๋ฒ ์ฌ๋ก: ==๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ์ด์ ๋ฅผ ๋งค์ฐ ๋ช
ํํ ์์ง ์๋ ํ, ํญ์ ===์ !==๋ฅผ ์ฌ์ฉํ์ธ์.
๋ฉด์ ๋ฌธ์ โ
๋ฌธ์ 1: ๊ธฐ๋ณธ ํ์ ๋น๊ตโ
๋ค์ ํํ์์ ๊ฒฐ๊ณผ๋ฅผ ํ๋จํ์ธ์:
1 == '1'; // ?
1 === '1'; // ?
๋ต:
1 == '1'; // true
1 === '1'; // false
ํด์ค:
==(๋๋ฑ ์ฐ์ฐ์): ํ์ ๋ณํ์ ์ํ- ๋ฌธ์์ด
'1'์ด ์ซ์1๋ก ๋ณํ๋จ - ๊ทธ ํ
1 == 1์ ๋น๊ตํ์ฌ ๊ฒฐ๊ณผ๋true
- ๋ฌธ์์ด
===(์๊ฒฉ ๋๋ฑ ์ฐ์ฐ์): ํ์ ๋ณํ์ ์ํํ์ง ์์number์string์ ํ์ ์ด ๋ค๋ฅด๋ฏ๋ก ๋ฐ๋กfalse๋ฅผ ๋ฐํ
ํ์ ๋ณํ ๊ท์น:
// == ์ ํ์
๋ณํ ์ฐ์ ์์
// 1. number๊ฐ ์์ผ๋ฉด ๋ค๋ฅธ ์ชฝ์ number๋ก ๋ณํ
'1' == 1; // '1' โ 1, ๊ฒฐ๊ณผ true
'2' == 2; // '2' โ 2, ๊ฒฐ๊ณผ true
'0' == 0; // '0' โ 0, ๊ฒฐ๊ณผ true
// 2. boolean์ด ์์ผ๋ฉด boolean์ number๋ก ๋ณํ
true == 1; // true โ 1, ๊ฒฐ๊ณผ true
false == 0; // false โ 0, ๊ฒฐ๊ณผ true
'1' == true; // '1' โ 1, true โ 1, ๊ฒฐ๊ณผ true
// 3. ๋ฌธ์์ด์์ ์ซ์๋ก์ ๋ณํ ํจ์
'' == 0; // '' โ 0, ๊ฒฐ๊ณผ true
' ' == 0; // ' ' โ 0, ๊ฒฐ๊ณผ true (๊ณต๋ฐฑ ๋ฌธ์์ด์ 0์ผ๋ก ๋ณํ๋จ)
๋ฌธ์ 2: null๊ณผ undefined ๋น๊ตโ
๋ค์ ํํ์์ ๊ฒฐ๊ณผ๋ฅผ ํ๋จํ์ธ์:
undefined == null; // ?
undefined === null; // ?
๋ต:
undefined == null; // true
undefined === null; // false
ํด์ค:
์ด๊ฒ์ JavaScript์ ํน์ ๊ท์น์ ๋๋ค:
undefined == null:true- ES ๋ช
์ธ์์ ํน๋ณํ ๊ท์ :
null๊ณผundefined๋==๋ก ๋น๊ตํ ๋ ๊ฐ์ - ์ด๊ฒ์ด
==๊ฐ ์ ์ฉํ ์ ์ผํ ์๋๋ฆฌ์ค: ๋ณ์๊ฐnull๋๋undefined์ธ์ง ํ์ธ
- ES ๋ช
์ธ์์ ํน๋ณํ ๊ท์ :
undefined === null:false- ์๋ก ๋ค๋ฅธ ํ์
(
undefined๋undefinedํ์ ,null์objectํ์ ) - ์๊ฒฉ ๋น๊ต ์ ๊ฐ์ง ์์
- ์๋ก ๋ค๋ฅธ ํ์
(
์ค๋ฌด ์์ฉ:
// ๋ณ์๊ฐ null ๋๋ undefined์ธ์ง ํ์ธ
function isNullOrUndefined(value) {
return value == null; // null๊ณผ undefined๋ฅผ ๋์์ ํ์ธ
}
isNullOrUndefined(null); // true
isNullOrUndefined(undefined); // true
isNullOrUndefined(0); // false
isNullOrUndefined(''); // false
// ๋๋ฑํ์ง๋ง ๋ ๊ฐ๊ฒฐํ ๋ฐฉ๋ฒ
function isNullOrUndefined(value) {
return value === null || value === undefined;
}
์ฃผ์ํ ํจ์ :
// null๊ณผ undefined๋ ์๋ก์๊ฒ๋ง ๊ฐ์
null == undefined; // true
null == 0; // false
null == false; // false
null == ''; // false
undefined == 0; // false
undefined == false; // false
undefined == ''; // false
// ํ์ง๋ง ===์์๋ ์๊ธฐ ์์ ์๊ฒ๋ง ๊ฐ์
null === null; // true
undefined === undefined; // true
null === undefined; // false
๋ฌธ์ 3: ์ข ํฉ ๋น๊ตโ
๋ค์ ํํ์์ ๊ฒฐ๊ณผ๋ฅผ ํ๋จํ์ธ์:
0 == false; // ?
0 === false; // ?
'' == false; // ?
'' === false; // ?
null == false; // ?
undefined == false; // ?
๋ต:
0 == false; // true (false โ 0)
0 === false; // false (ํ์
์ด ๋ค๋ฆ: number vs boolean)
'' == false; // true ('' โ 0, false โ 0)
'' === false; // false (ํ์
์ด ๋ค๋ฆ: string vs boolean)
null == false; // false (null์ null๊ณผ undefined์๋ง ๊ฐ์)
undefined == false; // false (undefined๋ null๊ณผ undefined์๋ง ๊ฐ์)
๋ณํ ํ๋ฆ๋:
// 0 == false ์ ๋ณํ ๊ณผ์
0 == false;
0 == 0; // false๋ฅผ ์ซ์ 0์ผ๋ก ๋ณํ
true; // ๊ฒฐ๊ณผ
// '' == false ์ ๋ณํ ๊ณผ์
'' == false;
'' == 0; // false๋ฅผ ์ซ์ 0์ผ๋ก ๋ณํ
0 == 0; // ''๋ฅผ ์ซ์ 0์ผ๋ก ๋ณํ
true; // ๊ฒฐ๊ณผ
// null == false ์ ํน์ ๊ฒฝ์ฐ
null == false;
// null์ ๋ณํ๋์ง ์์! ๋ช
์ธ์ ๋ฐ๋ผ, null์ null๊ณผ undefined์๋ง ๊ฐ์
false; // ๊ฒฐ๊ณผ
๋ฌธ์ 4: ๊ฐ์ฒด ๋น๊ตโ
๋ค์ ํํ์์ ๊ฒฐ๊ณผ๋ฅผ ํ๋จํ์ธ์:
[] == []; // ?
[] === []; // ?
{} == {}; // ?
{} === {}; // ?
๋ต:
[] == []; // false
[] === []; // false
{} == {}; // false
{} === {}; // false
ํด์ค:
- ๊ฐ์ฒด(๋ฐฐ์ด, ๊ฐ์ฒด ํฌํจ)์ ๋น๊ต๋ ์ฐธ์กฐ ๋น๊ต
- ๋ด์ฉ์ด ๊ฐ๋๋ผ๋, ๋ค๋ฅธ ๊ฐ์ฒด ์ธ์คํด์ค์ด๋ฉด ๊ฐ์ง ์์
==์===๋ ๊ฐ์ฒด์ ๋ํด ๋์ผํ๊ฒ ๋์ (๋ชจ๋ ์ฐธ์กฐ๋ฅผ ๋น๊ต)
// ์ฐธ์กฐ๊ฐ ๊ฐ์ ๋๋ง ๊ฐ์
const arr1 = [];
const arr2 = arr1; // ๊ฐ์ ๋ฐฐ์ด์ ๋ํ ์ฐธ์กฐ
arr1 == arr2; // true
arr1 === arr2; // true
// ๋ด์ฉ์ด ๊ฐ์ง๋ง ๋ค๋ฅธ ์ธ์คํด์ค
const arr3 = [1, 2, 3];
const arr4 = [1, 2, 3];
arr3 == arr4; // false (๋ค๋ฅธ ์ฐธ์กฐ)
arr3 === arr4; // false (๋ค๋ฅธ ์ฐธ์กฐ)
// ๊ฐ์ฒด๋ ๋ง์ฐฌ๊ฐ์ง
const obj1 = { name: 'Alice' };
const obj2 = { name: 'Alice' };
obj1 == obj2; // false
obj1 === obj2; // false
๋ฉด์ ๋น ๋ฅธ ์๊ธฐโ
==์ ํ์
๋ณํ ๊ท์น (์์์๋ถํฐ ์ฐ์ ):
null == undefinedโtrue(ํน์ ๊ท์น)number == stringโ string์ number๋ก ๋ณํnumber == booleanโ boolean์ number๋ก ๋ณํstring == booleanโ ๋ชจ๋ number๋ก ๋ณํ- ๊ฐ์ฒด๋ ์ฐธ์กฐ๋ฅผ ๋น๊ต, ๋ณํ ์์
===์ ๊ท์น (๊ฐ๋จ):
- ํ์
์ด ๋ค๋ฆ โ
false - ํ์ ์ด ๊ฐ์ โ ๊ฐ(๊ธฐ๋ณธ ํ์ ) ๋๋ ์ฐธ์กฐ(๊ฐ์ฒด ํ์ ) ๋น๊ต
๋ชจ๋ฒ ์ฌ๋ก:
// โ
ํญ์ === ์ฌ์ฉ
if (value === 0) {
}
if (name === 'Alice') {
}
// โ
์ ์ผํ ์์ธ: null/undefined ํ์ธ
if (value == null) {
// value๋ null ๋๋ undefined
}
// โ == ์ฌ์ฉ ํผํ๊ธฐ (์์ ์์ธ ์ ์ธ)
if (value == 0) {
} // ๋น์ถ์ฒ
if (name == 'Alice') {
} // ๋น์ถ์ฒ
๋ฉด์ ๋ต๋ณ ์์:
"
==๋ ํ์ ๋ณํ์ ์ํํ์ฌ0 == '0'์ดtrue๊ฐ ๋๋ ๊ฒ์ฒ๋ผ ์ง๊ด์ ๋ฐํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ด๋ํ ์ ์์ต๋๋ค.===๋ ์๊ฒฉ ๋น๊ต๋ก, ํ์ ๋ณํ์ ์ํํ์ง ์์ผ๋ฉฐ ํ์ ์ด ๋ค๋ฅด๋ฉด ๋ฐ๋กfalse๋ฅผ ๋ฐํํฉ๋๋ค.๋ชจ๋ฒ ์ฌ๋ก๋ ํญ์
===๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด๊ณ , ์ ์ผํ ์์ธ๋value == null๋กnull๊ณผundefined๋ฅผ ๋์์ ํ์ธํ ์ ์๋ค๋ ๊ฒ์ ๋๋ค.ํนํ ์ฃผ์ํ ์ ์
null == undefined๊ฐtrue์ด์ง๋งnull === undefined๋false๋ผ๋ ๊ฒ์ผ๋ก, ์ด๋ JavaScript์ ํน์ ๊ท์ ์ ๋๋ค."
2. What is the difference between && and || ? Please explain short-circuit evaluationโ
&&์||์ ์ฐจ์ด์ ์ ๋ฌด์์ธ๊ฐ์? ๋จ์ถ ํ๊ฐ์ ๋ํด ์ค๋ช ํด ์ฃผ์ธ์
๊ธฐ๋ณธ ๊ฐ๋ โ
&&(AND): ์ผ์ชฝ์ดfalsy์ผ ๋ ์ผ์ชฝ ๊ฐ์ ๋ฐ๋ก ๋ฐํํ๊ณ , ์ค๋ฅธ์ชฝ์ ์คํํ์ง ์์||(OR): ์ผ์ชฝ์ดtruthy์ผ ๋ ์ผ์ชฝ ๊ฐ์ ๋ฐ๋ก ๋ฐํํ๊ณ , ์ค๋ฅธ์ชฝ์ ์คํํ์ง ์์
๋จ์ถ ํ๊ฐ ์์โ
// && ๋จ์ถ ํ๊ฐ
const user = null;
const name = user && user.name; // user๊ฐ falsy์ด๋ฏ๋ก null์ ๋ฐ๋ก ๋ฐํ, user.name์ ์ ๊ทผํ์ง ์์
console.log(name); // null (์๋ฌ ์์)
// || ๋จ์ถ ํ๊ฐ
const defaultName = 'Guest';
const userName = user || defaultName; // user๊ฐ falsy์ด๋ฏ๋ก ์ค๋ฅธ์ชฝ์ defaultName์ ๋ฐํ
console.log(userName); // 'Guest'
// ์ค๋ฌด ์์ฉ
function greet(name) {
const displayName = name || 'Anonymous'; // name์ด ์ ๋ฌ๋์ง ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ ์ฌ์ฉ
console.log(`Hello, ${displayName}!`);
}
greet('Alice'); // Hello, Alice!
greet(); // Hello, Anonymous!
์์ฃผ ๋น ์ง๋ ํจ์ โ ๏ธโ
// ๋ฌธ์ : 0๊ณผ ''๋ falsy
const count = 0;
const result = count || 10; // 0์ falsy์ด๋ฏ๋ก 10์ ๋ฐํ
console.log(result); // 10 (์ํ๋ ๊ฒฐ๊ณผ๊ฐ ์๋ ์ ์์)
// ํด๊ฒฐ์ฑ
: ?? (Nullish Coalescing) ์ฌ์ฉ
const betterResult = count ?? 10; // null/undefined์ธ ๊ฒฝ์ฐ์๋ง 10์ ๋ฐํ
console.log(betterResult); // 0
3. What is the ?. (Optional Chaining) operator ?โ
Optional Chaining ์ฐ์ฐ์
?.๋ ๋ฌด์์ธ๊ฐ์?
๋ฌธ์ ์๋๋ฆฌ์คโ
๊ธฐ์กด ์์ฑ๋ฒ์ ์๋ฌ๊ฐ ๋ฐ์ํ๊ธฐ ์ฌ์:
const user = {
name: 'Alice',
address: {
city: 'Taipei',
},
};
// โ ์ํ: address๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด ์๋ฌ ๋ฐ์
console.log(user.address.city); // ์ ์
console.log(otherUser.address.city); // TypeError: Cannot read property 'city' of undefined
// โ
์์ ํ์ง๋ง ์ฅํฉํจ
const city = user && user.address && user.address.city;
Optional Chaining ์ฌ์ฉโ
// โ
๊ฐ๊ฒฐํ๊ณ ์์
const city = user?.address?.city; // 'Taipei'
const missingCity = otherUser?.address?.city; // undefined (์๋ฌ ์์)
// ๋ฉ์๋ ํธ์ถ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
user?.getName?.(); // getName์ด ์กด์ฌํ๋ฉด ์คํ
// ๋ฐฐ์ด์๋ ์ฌ์ฉ ๊ฐ๋ฅ
const firstItem = users?.[0]?.name; // ์ฒซ ๋ฒ์งธ ์ฌ์ฉ์์ ์ด๋ฆ์ ์์ ํ๊ฒ ์ ๊ทผ
์ค๋ฌด ์์ฉโ
// API ์๋ต ์ฒ๋ฆฌ
function displayUserInfo(response) {
const userName = response?.data?.user?.name ?? 'Unknown User';
const email = response?.data?.user?.email ?? 'No email';
console.log(`User: ${userName}`);
console.log(`Email: ${email}`);
}
// DOM ์กฐ์
const buttonText = document.querySelector('.submit-btn')?.textContent;
4. What is the ?? (Nullish Coalescing) operator ?โ
Nullish Coalescing ์ฐ์ฐ์
??๋ ๋ฌด์์ธ๊ฐ์?
||์์ ์ฐจ์ดโ
// ||๋ ๋ชจ๋ falsy ๊ฐ์ ๊ฑฐ์ง์ผ๋ก ์ทจ๊ธ
const value1 = 0 || 'default'; // 'default'
const value2 = '' || 'default'; // 'default'
const value3 = false || 'default'; // 'default'
// ??๋ null๊ณผ undefined๋ง ๋น ๊ฐ์ผ๋ก ์ทจ๊ธ
const value4 = 0 ?? 'default'; // 0
const value5 = '' ?? 'default'; // ''
const value6 = false ?? 'default'; // false
const value7 = null ?? 'default'; // 'default'
const value8 = undefined ?? 'default'; // 'default'
์ค๋ฌด ์์ฉโ
// 0์ด ๋ ์ ์๋ ์ซ์ ์ฒ๋ฆฌ
function updateScore(newScore) {
// โ
์ฌ๋ฐ๋ฆ: 0์ ์ ํจํ ์ ์
const score = newScore ?? 100; // 0์ด๋ฉด 0์ ์ ์ง, null/undefined์ธ ๊ฒฝ์ฐ์๋ง 100 ์ฌ์ฉ
return score;
}
updateScore(0); // 0
updateScore(null); // 100
updateScore(undefined); // 100
// ์ค์ ๊ฐ ์ฒ๋ฆฌ
const config = {
timeout: 0, // 0๋ฐ๋ฆฌ์ด๋ ์ ํจํ ์ค์
maxRetries: null,
};
const timeout = config.timeout ?? 3000; // 0 (0 ์ค์ ์ ์ ์ง)
const retries = config.maxRetries ?? 3; // 3 (null์ด๋ฉด ๊ธฐ๋ณธ๊ฐ ์ฌ์ฉ)
์กฐํฉ ์ฌ์ฉโ
// ??์ ?.๋ ์์ฃผ ํจ๊ป ์ฌ์ฉ๋จ
const userAge = user?.profile?.age ?? 18; // ๋์ด ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ 18
// ์ค๋ฌด ์ฌ๋ก: ํผ ๊ธฐ๋ณธ๊ฐ
function initForm(data) {
return {
name: data?.name ?? '',
age: data?.age ?? 0, // 0์ ์ ํจํ ๋์ด
isActive: data?.isActive ?? true,
};
}
5. What is the difference between i++ and ++i ?โ
i++์++i์ ์ฐจ์ด์ ์ ๋ฌด์์ธ๊ฐ์?
๊ธฐ๋ณธ ์ฐจ์ดโ
i++(ํ์): ํ์ฌ ๊ฐ์ ๋จผ์ ๋ฐํํ ํ 1์ ๋ํจ++i(์ ์): ๋จผ์ 1์ ๋ํ ํ ์๋ก์ด ๊ฐ์ ๋ฐํ
์์โ
let a = 5;
let b = a++; // b = 5, a = 6 (๋จผ์ b์ ํ ๋นํ ํ ์ฆ๊ฐ)
console.log(a, b); // 6, 5
let c = 5;
let d = ++c; // d = 6, c = 6 (๋จผ์ ์ฆ๊ฐํ ํ d์ ํ ๋น)
console.log(c, d); // 6, 6
์ค๋ฌด ์ํฅโ
// ๋ฃจํ์์๋ ๋ณดํต ์ฐจ์ด ์์ (๋ฐํ๊ฐ์ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก)
for (let i = 0; i < 5; i++) {} // โ
์ผ๋ฐ์
for (let i = 0; i < 5; ++i) {} // โ
์ด๊ฒ๋ ๊ฐ๋ฅ, ์ผ๋ถ๋ ์ฝ๊ฐ ๋ ๋น ๋ฅด๋ค๊ณ ์๊ฐ (์ค์ ๋ก ํ๋ JS ์์ง์์๋ ์ฐจ์ด ์์)
// ํ์ง๋ง ํํ์์์๋ ์ฐจ์ด๊ฐ ์์
let arr = [1, 2, 3];
let i = 0;
console.log(arr[i++]); // 1 (๋จผ์ i=0์ผ๋ก ๊ฐ์ ๊ฐ์ ธ์จ ํ i๊ฐ 1์ด ๋จ)
console.log(arr[++i]); // 3 (i๊ฐ ๋จผ์ 2๊ฐ ๋ ํ ๊ฐ์ ๊ฐ์ ธ์ด)
๋ชจ๋ฒ ์ฌ๋กโ
// โ
๋ช
ํํจ: ๋ถ๋ฆฌํด์ ์์ฑ
let count = 0;
const value = arr[count];
count++;
// โ ๏ธ ๋น์ถ์ฒ: ํผ๋ํ๊ธฐ ์ฌ์
const value = arr[count++];
6. What is the Ternary Operator ? When should you use it ?โ
์ผํญ ์ฐ์ฐ์๋ ๋ฌด์์ธ๊ฐ์? ์ธ์ ์ฌ์ฉํด์ผ ํ๋์?
๊ธฐ๋ณธ ๊ตฌ๋ฌธโ
condition ? valueIfTrue : valueIfFalse;
๊ฐ๋จํ ์์โ
// ๊ธฐ์กด if-else
let message;
if (age >= 18) {
message = 'Adult';
} else {
message = 'Minor';
}
// โ
์ผํญ ์ฐ์ฐ์: ๋ ๊ฐ๊ฒฐ
const message = age >= 18 ? 'Adult' : 'Minor';
์ ํฉํ ์ฌ์ฉ ์๋๋ฆฌ์คโ
// 1. ๊ฐ๋จํ ์กฐ๊ฑด๋ถ ํ ๋น
const status = isLoggedIn ? 'Online' : 'Offline';
// 2. JSX/ํ
ํ๋ฆฟ์์์ ์กฐ๊ฑด๋ถ ๋ ๋๋ง
return <div>{isLoading ? <Spinner /> : <Content />}</div>;
// 3. ๊ธฐ๋ณธ๊ฐ ์ค์ (๋ค๋ฅธ ์ฐ์ฐ์์ ์กฐํฉ)
const displayName = user?.name ?? 'Guest';
const greeting = isVIP ? `Welcome, ${displayName}!` : `Hello, ${displayName}`;
// 4. ํจ์ ๋ฐํ๊ฐ
function getDiscount(isMember) {
return isMember ? 0.2 : 0;
}
์ฌ์ฉ์ ํผํด์ผ ํ ์๋๋ฆฌ์คโ
// โ ์ค์ฒฉ์ด ๋๋ฌด ๊น์ด ์ฝ๊ธฐ ์ด๋ ค์
const result = condition1
? value1
: condition2
? value2
: condition3
? value3
: value4;
// โ
if-else ๋๋ switch๊ฐ ๋ ๋ช
ํ
let result;
if (condition1) result = value1;
else if (condition2) result = value2;
else if (condition3) result = value3;
else result = value4;
// โ ๋ณต์กํ ๋ก์ง
const canAccess =
user?.role === 'admin'
? true
: user?.permissions?.includes('read')
? true
: false;
// โ
์ฌ๋ฌ ์ค๋ก ๋ถ๋ฆฌ
const isAdmin = user?.role === 'admin';
const hasReadPermission = user?.permissions?.includes('read');
const canAccess = isAdmin || hasReadPermission;
๋น ๋ฅธ ์๊ธฐ ์นด๋โ
| ์ฐ์ฐ์ | ์ฉ๋ | ๊ธฐ์ต ํฌ์ธํธ |
|---|---|---|
=== | ์๊ฒฉ ๋๋ฑ | ํญ์ ์ด๊ฒ์ ์ฌ์ฉ, ==๋ ์๊ธฐ |
&& | ๋จ์ถ AND | ์ผ์ชฝ์ด ๊ฑฐ์ง์ด๋ฉด ๋ฉ์ถ๊ณ ๊ฑฐ์ง๊ฐ ๋ฐํ |
|| | ๋จ์ถ OR | ์ผ์ชฝ์ด ์ฐธ์ด๋ฉด ๋ฉ์ถ๊ณ ์ฐธ๊ฐ ๋ฐํ |
?. | Optional Chaining | ์์ ํ ์ ๊ทผ, ์๋ฌ ์์ |
?? | Nullish Coalescing | null/undefined๋ง ์ฒ๋ฆฌ |
++i / i++ | ์๊ธฐ ์ฆ๊ฐ | ์ ์๋ ๋จผ์ ์ฆ๊ฐ, ํ์๋ ๋์ค์ ์ฆ๊ฐ |
? : | ์ผํญ ์ฐ์ฐ์ | ๊ฐ๋จํ ์กฐ๊ฑด์ ์ฌ์ฉ, ์ค์ฒฉ์ ํผํ๊ธฐ |