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.
Discussions

Question on if/when to use Ternary Operators?
Use โ€œifโ€ for statements and use ternary for expressions. For example, the appropriate way to use the ternary would go like this: console.log(nightTime ? "Nighttime" : "Daytime") Notice that this ternary is an expression that evaluates to a value. More on reddit.com
๐ŸŒ r/learnjavascript
17
4
December 29, 2024
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
1
0
July 12, 2019
What are pros/cons of ternary conditional operators? - Programming Language Design and Implementation Stack Exchange
There are some languages that support ternary operator. There are Python, C, JavaScript, PHP. In languages like C and JavaScript, the syntax is like this: condition ? do_true : do_false Python has More on langdev.stackexchange.com
๐ŸŒ langdev.stackexchange.com
June 23, 2023
[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
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. The conditional operator is also known as the ternary operator.
๐ŸŒ
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
Comparison operators can be used in conditional statements to compare values and take action depending on the result:
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ question on if/when to use ternary operators?
r/learnjavascript on Reddit: Question on if/when to use Ternary Operators?
December 29, 2024 -

So i am completly new to programming and learning thru codecademy.

I just got thru all the lessons about if else statements and when/how to use them but in their next lession they talk about Ternary Operator basically being a "shot handed" version of wirting an if else statement (if I am understanding that correctly) if I am understanding it correctly then my question is, is one more "professional" then the other or is it just based on what your coding or what lets say your boss is asking you to code

The other reason I ask is I want to devlope good habits now vs later down the road so using the example below is it I guess from a "real world" working senario is it better to use one over the other

For example; I know this is a very genaric and basic example but

let nightTime = true

if (nighTime) {
    console.log('Nightime');
} else {
    console.log('Daytime')
}

vs

nightTime
    ? console.log('Nighttime')
    : console.log('Daytime');
๐ŸŒ
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.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Use the Conditional (Ternary) Operator Why? - JavaScript - The freeCodeCamp Forum
July 12, 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โ€ฆ
๐ŸŒ
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.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Ternary_conditional_operator
Ternary conditional operator - Wikipedia
January 27, 2026 - The operator is similar to the way conditional expressions (if-then-else) work in functional programming languages, like Scheme, ML, Haskell, and XQuery, since if-then-else forms an expression instead of a statement in those languages.
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-course-conditional-operator-in-javascript
JavaScript Course Conditional Operator in JavaScript - GeeksforGeeks
July 11, 2025 - It works similarly to an if-else, where based on a condition we evaluate on the result. In the above code snippet if the 'condition' evolves to 'true' then 'value1' will be executed otherwise 'value2' will be executed.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ Nullish_coalescing
Nullish coalescing operator (??) - JavaScript | MDN
The nullish coalescing operator can be seen as a special case of the logical OR (||) operator. The latter returns the right-hand side operand if the left operand is any falsy value, not only null or undefined. In other words, if you use || to provide some default value to another variable foo, you may encounter unexpected behaviors if you consider some falsy values as usable (e.g., '' or 0).
๐ŸŒ
CodeParrot
codeparrot.ai โ€บ blogs โ€บ beyond-if-else-javascripts-ternary-operator-explained
Beyond If-Else: JavaScript's Ternary Operator Explained
March 23, 2025 - The ternary operator (also known as the conditional operator) is one of those little JavaScript features that can make your code cleaner and more readable when used correctly.
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ ternary-operator
JavaScript Ternary Operator (with Examples)
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.