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
🌐
Medium
medium.com › @ansekfzhd5455 › the-complete-javascript-ternary-conditional-operator-55b78ede86aa
[The Complete JavaScript] Ternary(Conditional) Operator | by Amy Crescent Moon | Medium
March 21, 2023 - The conditional(ternary) operator allows us to write something similar to an if/else statement, but all in one line. But instead of using a statement, we use this conditional operator(= the ternary…
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

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 7, 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
🌐
freeCodeCamp
freecodecamp.org › news › javascript-if-else-and-if-then-js-conditional-statements
JavaScript If-Else and If-Then – JS Conditional Statements
August 9, 2021 - In the logical AND (&&) operator, if both conditions are true, then the if block will be executed.
🌐
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 › 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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
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_if_ternary.asp
JavaScript Conditional Ternary Operator
The conditional operator is a shorthand for writing conditional if...else statements.
🌐
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.
🌐
Medium
medium.com › @20nicoll-oliver17 › ternary-operator-conditional-breakdown-ad8e02a0d27
Ternary Operator — Conditional Breakdown | by Nicoll Oliver
June 9, 2021 - The conditional (ternary) operator ... 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...
🌐
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');
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Use the Conditional (Ternary) Operator Why? - JavaScript - The freeCodeCamp Forum
July 7, 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…
🌐
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.
🌐
Wikipedia
en.wikipedia.org › wiki › Ternary_conditional_operator
Ternary conditional operator - Wikipedia
2 days ago - A true ternary operator exists for arithmetic expressions: ((result = condition ? value_if_true : value_if_false)) ... Where condition can be any bash command. When it exits with success, the first echo command is executed, otherwise the second one is executed. The following code in C assigns result to the value of x if a > b, and otherwise to the value of y. This is the same syntax as in many related languages including C++, Java, JavaScript, and Dart.
🌐
Medium
medium.com › @gecno › choosing-between-if-else-statements-and-ternary-operators-in-javascript-ba9449953fbf
Choosing Between If-Else Statements and Ternary Operators in JavaScript | by Gec | Medium
October 19, 2023 - They’re ideal for simple, one-liner conditions. The syntax is compact and elegant, making your code easier to read and maintain. Here’s the same example using a ternary operator: let isRaining = true; let message = isRaining ? "Don't forget your umbrella!" : "Enjoy the sunshine!"; console.log(message); Now, the big question is when to use one over the other. ... In conclusion, the choice between if-else statements and ternary operators in JavaScript depends on the complexity of the condition and the need for code readability.