| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Operators/Conditional_operator | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its 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
exprIfTruecondition truthy (true , true )
exprIfFalsecondition 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 :
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 2027 LanguageSpecification # sec-conditional-operator |
This page was last modified on 2025 2 11 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |