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.
🌐
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.
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
47
187
July 14, 2021
Use the Conditional (Ternary) Operator Why?
Tell us what’s happening: Why should ternary conditional operator be used? I find the conditional “if” more comfortable. Similarly, I would appreciate someone to explain what the differences are, in addition to the syntax and in what other cases it would be useful or more efficient to ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
4
0
August 13, 2019
[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
How to use IF condition in Javascript Map function?
You can also use filter to first narrow the array down to elements you want to be operated on by map. More on reddit.com
🌐 r/learnjavascript
16
7
October 27, 2022
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.

🌐
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.
🌐
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   January 15, 2026
🌐
W3Schools
w3schools.com › js › js_comparisons.asp
JavaScript Comparison Operators
You will learn more about the use of conditional statements in the if...else chapter of this tutorial. All the comparison operators above can also be used on strings: let text1 = "A"; let text2 = "B"; let result = text1 < text2; Try it Yourself » ... Comparing data of different types may give unexpected results. When comparing a string with a number, JavaScript ...
Find elsewhere
🌐
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.
🌐
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.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Conditional branching: if, '?'
The operator is represented by a question mark ?. Sometimes it’s called “ternary”, because the operator has three operands. It is actually the one and only operator in JavaScript which has that many. ... The condition is evaluated: if it’s truthy then value1 is returned, otherwise – value2.
🌐
Medium
medium.com › @ykods › understanding-the-ternary-operator-in-javascript-b2b7b2184321
Understanding the Ternary Operator in JavaScript | Yeran Kods | Medium
February 7, 2025 - The ternary operator (? :) is a shorthand way of writing an if-else statement in JavaScript and many other programming languages. It is often used to make conditional assignments more concise and readable.
🌐
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.
🌐
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 - Its a good idea to only use one ternary at a time. You're future self in a bind will thank you. Readability is important ... Never knew that ternary could be used like this. As someone else suggested, the ternary operator is normally for just two comparisons like "if it's not this, then it's this".
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Use the Conditional (Ternary) Operator Why? - JavaScript
August 13, 2019 - Tell us what’s happening: Why should ternary conditional operator be used? I find the conditional “if” more comfortable. Similarly, I would appreciate someone to explain what the differences are, in addition to the synt…
🌐
Built In
builtin.com › software-engineering-perspectives › ternary-operator-js
Ternary Operator in JavaScript Explained | Built In
July 13, 2023 - The ternary operator is a concise way of expressing conditional statements in a single line of JavaScript code. Our expert explains how it works.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-operators
JavaScript Operators - GeeksforGeeks
condition ? expression1 : expression2 evaluates expression1 if the condition is true, otherwise evaluates expression2. Comma Operator (,) mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand. ... The final result of the expression is the rightmost value. Unary operators operate on a single operand (e.g., increment, decrement). ... JavaScript Relational operators are used to compare its operands and determine the relationship between them.
Published   May 5, 2026
🌐
Lsupathways
coding-for-the-web.lsupathways.org › 2_operators › conditional_operators
Conditional and Logical Operators :: Educational material for Coding for the Web
These can be used to assign boolean values to variables or to determine the next course of action in conditional statements. ... x = 20; y = 30; z = x > 10 || y < 15; //returns true because the left side is true, even though the right side is false · Operator: !
🌐
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.
🌐
Refine
refine.dev › home › blog › tutorials › how to use the 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.
🌐
Quora
quora.com › In-JavaScript-is-much-better-use-ternary-if-instead-of-if-else
In JavaScript is much better use ternary if instead of if/else? - Quora
Answer (1 of 2): No. It has its places and, I have to admit, I use it very frequently. It results in more dense code which is, if it stays readable, better than verbose code. Readability is a big point here. As Orion Kindel pointed out, its semantic is different than a regular IF/ELSE. This wo...
🌐
freeCodeCamp
freecodecamp.org › news › the-ternary-operator-in-javascript
JavaScript Ternary Operator – Syntax and Example Use Case
January 6, 2023 - There are many operators in JavaScript, one of which is the ternary operator. In this article, I'll explain what this operator is, and how it can be useful when building applications.