Naši dobrovolníci ještě tento článek do jazyka Čeština nepřeložili. Přidejte se a pomozte nám tuto práci dokončit!
Tento článek si můžete přečíst také v jazyce English (US).

The conditional (ternary) operator is the only JavaScript operator that takes three operands. This operator is frequently used as a shortcut for the if statement.

Syntax

condition ? exprIfTrue : exprIfFalse 

Parameters

condition
An expression whose value is used as a condition.
exprIfTrue
An expression which is evaluated if the condition evaluates to a truthy value (one which equals or can be converted to true).
exprIfFalse
An expression which is executed if the condition is falsy (that is, has a value which can be converted to false).

Description

Besides false, possible falsy expressions are: nullNaN0, the empty string (""), and undefined. If condition is any of these, the result of the conditional expression will be exprIfFalse.

A simple example:

var age = 26;
var beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage); // "Beer"

One common usage is to handle a value that may be null:

function greeting(person) {
    var name = person ? person.name : "stranger";
    return "Howdy, " + name;
}

console.log(greeting({name: 'Alice'}));  // "Howdy, Alice"
console.log(greeting(null));             // "Howdy, stranger"​​​​​

Note: The optional chaining operator has been designed to handle this use case. At the time of writing (July 2019), this is still experimental and not implemented.

Conditional chains

The ternary operator is right-associative, which means it can be "chained" in the following way, similar to an if … else if … else if … else chain:

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

// Equivalent to:

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

Specifications

Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
The definition of 'Conditional Operator' in that specification.
Draft
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Conditional Operator' in that specification.
Standard
ECMAScript 5.1 (ECMA-262)
The definition of 'The conditional operator' in that specification.
Standard
ECMAScript 1st Edition (ECMA-262)
The definition of 'The conditional operator' in that specification.
Standard Initial definition. Implemented in JavaScript 1.0.

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Conditional operator (c ? t : f)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support

See also