ì¡°ê±´ (ì¼í) ì°ì°ì
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2015ë 7ìâ©.
ì¡°ê±´ (ì¼í) ì°ì°ìë JavaScriptìì ì¸ ê°ì í¼ì°ì°ì를 ë°ë ì ì¼í ì°ì°ìì
ëë¤. ìììë¶í° 조건문, 물ìí(?), ì¡°ê±´ë¬¸ì´ ì°¸(truthy)ì¼ ê²½ì° ì¤íí ííì, ì½ë¡ (:), ì¡°ê±´ë¬¸ì´ ê±°ì§(falsy)ì¼ ê²½ì° ì¤íí ííìì´ ë°°ì¹ë©ëë¤. í´ë¹ ì°ì°ìë if...else문ì ëì²´ì¬ë¡ ë¹ë²í ì¬ì©ë©ëë¤.
ìëí´ ë³´ê¸°
function getFee(isMember) {
return isMember ? "$2.00" : "$10.00";
}
console.log(getFee(true));
// Expected output: "$2.00"
console.log(getFee(false));
// Expected output: "$10.00"
console.log(getFee(null));
// Expected output: "$10.00"
구문
condition ? exprIfTrue : exprIfFalse;
매ê°ë³ì
condition-
조건문ì¼ë¡ ì¬ì©ëë ííì
exprIfTrue-
conditionì´ truthyí ê°ì¼ë¡ íê°ë ê²½ì° ì¤íëë ííì (trueì ê°ê±°ë,trueë¡ ì¹íë ì ìë ê°) exprIfFalse-
conditionì´ falsyí ê°ì¼ë¡ íê°ë ê²½ì° ì¤íëë ííì (falseì ê°ê±°ë,falseë¡ ì¹íë ì ìë ê°)
ì¤ëª
false ì´ì¸ì falsyí ííììë null, NaN, 0, ë¹ì´ìë 문ìì´ (""), ê·¸ë¦¬ê³ undefinedê° ììµëë¤. conditionì´ ì´ ì¤ íëì¼ ê²½ì° ì¡°ê±´ ì°ì°ìì ê²°ê´ê°ì exprIfFalse ííìì ì¤íí ê²°ê´ê°ì
ëë¤.
ìì
>ê°ë¨í ìì
var age = 26;
var beverage = age >= 21 ? "Beer" : "Juice";
console.log(beverage); // "Beer"
null ê° ì²ë¦¬í기
nullì¼ ì ìë ê°ì ì²ë¦¬í ë íí ì¬ì©ë©ëë¤:
let greeting = (person) => {
let name = person ? person.name : `stranger`;
return `Howdy, ${name}`;
};
console.log(greeting({ name: `Alice` })); // "Howdy, Alice"
console.log(greeting(null)); // "Howdy, stranger"
ì°ê²°ë 조건문 ì²ë¦¬í기
ì¡°ê±´ ì°ì°ìë ìëì ê°ì´ ì°ê²°í´ ì¬ì©í ì ììµëë¤. ì´ë ì°ê²°ë if ⦠else if ⦠else if ⦠elseì ì ì¬í©ëë¤.
function example(â¦) {
return condition1 ? value1
: condition2 ? value2
: condition3 ? value3
: value4;
}
ì ì½ëë ìëì ì°ê²°ë if ⦠elseì ëë±í©ëë¤.
function example(â¦) {
if (condition1) { return value1; }
else if (condition2) { return value2; }
else if (condition3) { return value3; }
else { return value4; }
}
ëª ì¸
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-conditional-operator > |