[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Conditional_operator [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Baseline

20157

JavaScript 3 (?) (:) if...else

function getFee(isMember) {
  return isMember ? "$2.00" : "$10.00";
}

console.log(getFee(true));
// : "$2.00"

console.log(getFee(false));
// : "$10.00"

console.log(getFee(null));
// : "$10.00"

js
condition ? exprIfTrue : exprIfFalse

condition

exprIfTrue

condition (true true )

exprIfFalse

condition (false false )

false null, NaN, 0, (""), undefined condition exprIfFalse

js
const age = 26;
const beverage = age >= 21 ? "" : "";
console.log(beverage); // ""

null

null

js
const greeting = (person) => {
  const name = person ? person.name : "";
  return `${name}`;
};

console.log(greeting({ name: "" })); // ""
console.log(greeting(null)); // ""

if else if else if else

js
function example() {
    return condition1 ? value1
         : condition2 ? value2
         : condition3 ? value3
         : value4;
}

if...else

js
function example() {
  if (condition1) {
    return value1;
  } else if (condition2) {
    return value2;
  } else if (condition3) {
    return value3;
  } else {
    return value4;
  }
}

ECMAScript 2027 LanguageSpecification
# sec-conditional-operator


Web Proxy Viewer  |  New URL  |  Original Page