This is a one-line shorthand for an if-else statement. It's called the conditional operator.1

Here is an example of code that could be shortened with the conditional operator:

var userType;
if (userIsYoungerThan18) {
  userType = "Minor";
} else {
  userType = "Adult";
}

if (userIsYoungerThan21) {
  serveDrink("Grape Juice");
} else {
  serveDrink("Wine");
}

This can be shortened with the ?: like so:

var userType = userIsYoungerThan18 ? "Minor" : "Adult";

serveDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine");

Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of minification:

userIsYoungerThan21 ? serveGrapeJuice() : serveWine();

They can even be chained:

serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine');

Be careful, though, or you will end up with convoluted code like this:

var k = a ? (b ? (c ? d : e) : (d ? e : f)) : f ? (g ? h : i) : j;

1 Often called "the ternary operator," but in fact it's just a ternary operator [an operator accepting three operands]. It's the only one JavaScript currently has, though.

Answer from Peter Olson on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.
Top answer
1 of 16
771

This is a one-line shorthand for an if-else statement. It's called the conditional operator.1

Here is an example of code that could be shortened with the conditional operator:

var userType;
if (userIsYoungerThan18) {
  userType = "Minor";
} else {
  userType = "Adult";
}

if (userIsYoungerThan21) {
  serveDrink("Grape Juice");
} else {
  serveDrink("Wine");
}

This can be shortened with the ?: like so:

var userType = userIsYoungerThan18 ? "Minor" : "Adult";

serveDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine");

Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of minification:

userIsYoungerThan21 ? serveGrapeJuice() : serveWine();

They can even be chained:

serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine');

Be careful, though, or you will end up with convoluted code like this:

var k = a ? (b ? (c ? d : e) : (d ? e : f)) : f ? (g ? h : i) : j;

1 Often called "the ternary operator," but in fact it's just a ternary operator [an operator accepting three operands]. It's the only one JavaScript currently has, though.

2 of 16
183

I want to add some to the given answers.

In case you encounter (or want to use) a ternary in a situation like 'display a variable if it's set, else...', you can make it even shorter, without a ternary.


Instead of:

var welcomeMessage  = 'Hello ' + (username ? username : 'guest');

You can use:

var welcomeMessage  = 'Hello ' + (username || 'guest');

This is Javascripts equivallent of PHP's shorthand ternary operator ?:

Or even:

var welcomeMessage  = 'Hello ' + (username || something || maybethis || 'guest');

It evaluates the variable, and if it's false or unset, it goes on to the next.

🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Expressions_and_operators
Expressions and operators - JavaScript | MDN
The conditional operator is the only JavaScript operator that takes three operands. The operator can have one of two values based on a condition.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_conditional_operators.htm
JavaScript - Conditional Operators
The conditional operator in JavaScript first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation. The conditional operator is also known as the ternary operator.
🌐
W3Schools
w3schools.com › js › js_if_ternary.asp
JavaScript Conditional Ternary Operator
The conditional operator is a shorthand for writing conditional if...else statements. It is called a ternary operator because it takes three operands. ... The conditional (ternary) operator is the only JavaScript operator that takes three operands.
🌐
Programiz
programiz.com › javascript › ternary-operator
JavaScript Ternary Operator (with Examples)
A ternary operator can be used to replace an if..else statement in certain situations. Before you learn about ternary operators, be sure to check the JavaScript if...else tutorial. A ternary operator evaluates a condition and executes a block of code based on the condition.
🌐
W3Schools
w3schools.com › js › js_comparisons.asp
JavaScript Comparison Operators
Comparison operators can be used in conditional statements to compare values and take action depending on the result:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-ternary-operator
JavaScript Ternary Operator - GeeksforGeeks
The Ternary Operator in JavaScript is a conditional operator that evaluates a condition and returns one of two values based on whether the condition is true or false.
Published   July 28, 2025
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Conditional branching: if, '?'
To do that, we can use the if statement and the conditional operator ?, that’s also called a “question mark” operator. The if(...) statement evaluates a condition in parentheses and, if the result is true, executes a block of code.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-course-conditional-operator-in-javascript
JavaScript Course Conditional Operator in JavaScript - GeeksforGeeks
July 11, 2025 - JavaScript Conditional Operators allow us to perform different types of actions according to different conditions. We make use of the 'if' statement. ... The above argument named 'expression' is basically a condition that we pass into the 'if' ...
🌐
W3Resource
w3resource.com › javascript › operators › conditional.php
JavaScript : Conditional Operator - w3resource
In the following web document the conditional operator statement [status = (marks >= 30) ? "Pass" : "Fail"] assigns value "Pass" to the variable status if marks are 30 or more. Otherwise, it assigns the value of "Fail" to status. ... <!doctype html><head> <meta charset="utf-8"> <title>JavaScript conditional operator example with DOM</title> <meta name="description" content="This document contains an example of JavaScript conditional operator with DOM" /> </head> <form name="example" onsubmit="ViewOutput()"> <input type="text" id="marks" placeholder="Enter Marks" /> <input type="submit" value="Submit" /> </form> <body> <script src="javascript-conditional-operator-example1.js"> </script> </body> </html>
🌐
Unibo
lia.disi.unibo.it › materiale › JS › developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_Operator.html
Conditional (ternary) Operator - JavaScript | MDN
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.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-ternary-operator-explained
How to Use the Ternary Operator in JavaScript – Explained with Examples
February 27, 2024 - A ternary operator is a conditional operator in JavaScript that evaluates a conditional expression and returns either a truthy or falsy value.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-the-ternary-operator-in-javascript
How to Use the Ternary Operator in JavaScript – JS Conditional Example
February 27, 2023 - The ternary operator (?:), also known as the conditional operator, is a shorthand way of writing conditional statements in JavaScript – you can use a ternary operator instead of an if..else statement.
🌐
Can I Use
caniuse.com › mdn-javascript_operators_conditional
JavaScript operator: Conditional operator (`c ? t : f`) | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
🌐
LabEx
labex.io › tutorials › html-use-conditional-operator-in-javascript-451084
Use Conditional Operator in JavaScript | LabEx
Learn how to use the conditional (ternary) operator in JavaScript to create concise conditional statements and simplify decision-making logic in your code.
🌐
Built In
builtin.com › software-engineering-perspectives › ternary-operator-js
Ternary Operator in JavaScript Explained | Built In
July 13, 2023 - The ternary operator gets its name by being the only operator in JavaScript that takes three operands, or parts. The first part of a ternary operation is a logical condition that returns a true or false value. Then, after a question mark, come two expressions, separated by a colon.
🌐
Refine
refine.dev › blog › javascript-ternary-operator
How to Use the JavaScript Ternary Operator | Refine
October 8, 2024 - The syntax for the JavaScript Ternary Operator looks like this: conditionalExpression ? exprIfTruthy : exprIfFalsy; As we can see, the Ternary Operator places the conditional expression before the ? and accepts the executable expressions as two other operands that are separated by a colon, :. If the conditionalExpression evaluates to a truthy value, exprIfTruthy is executed. If it evaluates to a falsy value, exprIfFalsy is executed. In JavaScript, a truthy value corresponds to a value that is considered equivalent to true had that been converted to a Boolean.
🌐
Medium
medium.com › @louistrinh › ternary-operators-in-javascript-explained-when-to-use-them-30ba7e58b0f9
Ternary Operators in JavaScript: Explained & When to Use Them | by Louis Trinh | Medium
June 30, 2024 - In JavaScript, the ternary operator (also known as the conditional operator) is a concise way to write a simple if...else statement in a single line.