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

() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

() - JavaScript, : , (?), , , , (:), , , , , . if...else.

In this article

 ? 1 : 2

, true false.

1, 2

, .

1, , 2 . , , isMember, :

js
function getFee(isMember) {
  return "The fee is " + (isMember ? "$2.00" : "$10.00");
}

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

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

:

js
const elvisLives = Math.PI > 4 ? "" : "";
console.log(elvisLives); // ""

( : ):

js
const firstCheck = false;
const secondCheck = false;
const access = firstCheck
  ? " "
  : secondCheck
    ? " "
    : " ";

console.log(access); //    " "

- :

js
const age = 16;
let stop = false;

age > 18 ? location.assign("continue.html") : (stop = true);

, :

js
const age = 23;
let stop = false;

age > 18
  ? (alert(",   ."), location.assign("continue.html"))
  : ((stop = true), alert(",    !"));

. , , .

js
const age = 16;

const url =
  age > 18
    ? (alert(",   ."),
      // alert  "undefined",    ,  
      //      ,  
      "continue.html") //   ,  age > 18
    : (alert("  !"), alert(" :-("), "stop.html"); //   ,  age < 18

location.assign(url); // "stop.html"

Specification
ECMAScript 2027 LanguageSpecification
# sec-conditional-operator


Web Proxy Viewer  |  New URL  |  Original Page