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.

Discussions

Im trying to understand the "Conditional (ternary) operator." Do these 2 codes mean the same thing? I wrote the if statement
Yeah, but please do not code like that. Keep Ternary statements simple, otherwise use something else (if or switch) More on reddit.com
🌐 r/learnjavascript
46
187
July 14, 2021
[AskJS] Nullish Check in conditional
value == null only matches null and undefined, not any other falsy values. This is the only time you should use == over ===. More on reddit.com
🌐 r/javascript
23
7
August 16, 2024
Conditionally spreading objects in JavaScript
2.4M subscribers in the javascript community. Chat about javascript and javascript related projects. Yes, typescript counts. Please keep self promotion to a minimum/reasonable level. More on reddit.com
🌐 r/javascript
44
152
September 19, 2022
how to wrap a map return in a div using conditional rendering w ternary operator in react?
You could just change the className depending on history.length and probably use a wrapper . Or maybe like this {history.length ? ( {history .slice(Math.max(history.length - 10, 0)) .map((entry, index) => { return

{entry}

; }) } ) : ( Go do! )} More on reddit.com
🌐 r/reactjs
11
0
December 23, 2023

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
🌐
Wikipedia
en.wikipedia.org › wiki › Ternary_conditional_operator
Ternary conditional operator - Wikipedia
1 month ago - Although many ternary operators are theoretically possible, the conditional operator is commonly used and other ternary operators rare, so the conditional variant is commonly referred to as the ternary operator. Typical syntax for an expression using the operator is like if a then b else c or a ? b : c. One can read it aloud as "if a then b otherwise c". The form a ? b : c is the most common, but alternative syntax exists. For example, Raku uses the syntax a ?? b !! c to avoid confusion with the infix operators ?
🌐
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 › c › c_conditions_short_hand.php
C Short Hand If ... Else (Ternary Operator)
C Examples C Real-Life Examples C Exercises C Quiz C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... There is also a short-hand if...else, known as the ternary operator because it uses three operands. The ternary operator returns a value based on a condition: if the condition is true, it returns the first value; otherwise, it returns the second value.
🌐
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.
Find elsewhere
🌐
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' ...
🌐
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.
🌐
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.
🌐
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
🌐
Reddit
reddit.com › r/learnjavascript › im trying to understand the "conditional (ternary) operator." do these 2 codes mean the same thing? i wrote the if statement
r/learnjavascript on Reddit: Im trying to understand the "Conditional (ternary) operator." Do these 2 codes mean the same thing? I wrote the if statement
July 14, 2021 - Five pieces of my advice on implementing the ternary conditional `?:` operator in your programming language ... This subreddit is dedicated to the theory, design and implementation of programming languages. ... A subreddit for all questions related to programming in any language. ... Share strange or straight-up awful code. ... This subreddit is for anyone who wants to learn JavaScript ...
🌐
Mimo
mimo.org › glossary › javascript › ternary-operator
JavaScript Ternary Operator: Syntax, Usage, and Examples
The JavaScript ternary operator, also known as the conditional operator, offers a quick and compact way to write conditional expressions. Instead of using a full if...else statement, you can evaluate a condition and return a value in a single line.
🌐
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
April 15, 2015 - 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.
🌐
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.
🌐
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.
🌐
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:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › if...else
if...else - JavaScript | MDN
The if...else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the optional else clause will be executed.
🌐
React
react.dev › learn › conditional-rendering
Conditional Rendering – React
You’d have to do it in two places in your code! In such a situation, you could conditionally include a little JSX to make your code more DRY. JavaScript has a compact syntax for writing a conditional expression — the conditional operator or “ternary operator”.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Conditionals
Making decisions in your code — conditionals - Learn web development | MDN
August 18, 2025 - The ternary or conditional operator is a small bit of syntax that tests a condition and returns one value/expression if it is true, and another if it is false — this can be useful in some situations, and can take up a lot less code than an if...else block if you have two choices that are ...