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. Answer from MoTTs_ on reddit.com
๐ŸŒ
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

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
5
December 29, 2024
jquery - JavaScript ternary operator example with functions - Stack Overflow
I am using jQuery 1.7.1 I am just starting to use the JavaScript ternary operator to replace simple if/else statements. I have done so successfully in several places. I was surprised when I More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do you use the ? : (conditional) operator in JavaScript? - Stack Overflow
If javascript only has 1 of a type of operator, then it is definitely correct to say THE ternary operator and not A ternary operator... Saying "this ternary operator is A ternary operator in javascript (and it is the only one)" is silly, just use THE and it implies all of that. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Ternary operator
In the article How to Build a Responsive Form with Filter Functionality Using HTML, CSS, and JavaScript Iโ€™m having trouble converting the ternary operator back to a standard if/else statement. It would help me understand the ternary operator if I could reverse engineer it so to speak. More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
8
0
April 14, 2025
๐ŸŒ
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
๐ŸŒ
Jrsinclair
jrsinclair.com โ€บ articles โ€บ 2021 โ€บ rethinking-the-javascript-ternary-operator
Rethinking the JavaScript ternary operator
March 15, 2021 - With the ternary operator, the second and third expressions can be any type. But the interpreter will always cast the first to a boolean. Itโ€™s unique. So, as far as operators go, itโ€™s odd. 1 To be fair, in JavaScript, boolean operators donโ€™t cast the second expression.
๐ŸŒ
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');
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ the-ternary-operator-in-javascript
JavaScript Ternary Operator โ€“ Syntax and Example Use Case
January 6, 2023 - The ternary operator is a conditional operator which evaluates either of two expressions โ€“ a true expression and a false expression โ€“ based on a conditional expression that you provide.
๐ŸŒ
DEV Community
dev.to โ€บ megdiv โ€บ javascript-ternary-operator-kmo
JavaScript Ternary Operator - DEV Community
November 8, 2025 - One of my favorite parts of the JavaScript language is the JavaScript Ternary Operator. At heart, itโ€™s a succinct, clean way of writing an if/else statement. You start by asking a question, "Is this item a circle?" If the item is a circle, it takes path one If the item is not a circle, it takes path two ... A ternary operator can also be stacked or โ€˜nestedโ€™. In this example the item weโ€™ll use is a triangle.
๐ŸŒ
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.
Top answer
1 of 6
199

Heh, there are some pretty exciting uses of ternary syntax in your question; I like the last one the best...

x = (1 < 2) ? true : false;

The use of ternary here is totally unnecessary - you could simply write

x = (1 < 2);

Likewise, the condition element of a ternary statement is always evaluated as a Boolean value, and therefore you can express:

(IsChecked == true) ? removeItem($this) : addItem($this);

Simply as:

(IsChecked) ? removeItem($this) : addItem($this);

In fact, I would also remove the IsChecked temporary as well which leaves you with:

($this.hasClass("IsChecked")) ? removeItem($this) : addItem($this);

As for whether this is acceptable syntax, it sure is! It's a great way to reduce four lines of code into one without impacting readability. The only word of advice I would give you is to avoid nesting multiple ternary statements on the same line (that way lies madness!)

2 of 6
24

The ternary style is generally used to save space. Semantically, they are identical. I prefer to go with the full if/then/else syntax because I don't like to sacrifice readability - I'm old-school and I prefer my braces.

The full if/then/else format is used for pretty much everything. It's especially popular if you get into larger blocks of code in each branch, you have a muti-branched if/else tree, or multiple else/ifs in a long string.

The ternary operator is common when you're assigning a value to a variable based on a simple condition or you are making multiple decisions with very brief outcomes. The example you cite actually doesn't make sense, because the expression will evaluate to one of the two values without any extra logic.

Good ideas:

this > that ? alert(this) : alert(that);  //nice and short, little loss of meaning

if(expression)  //longer blocks but organized and can be grasped by humans
{
    //35 lines of code here
}
else if (something_else)
{
    //40 more lines here
}
else if (another_one)  /etc, etc
{
    ...

Less good:

this > that ? testFucntion() ? thirdFunction() ? imlost() : whathappuh() : lostinsyntax() : thisisprobablybrokennow() ? //I'm lost in my own (awful) example by now.
//Not complete... or for average humans to read.

if(this != that)  //Ternary would be done by now
{
    x = this;
}
else
}
    x = this + 2;
}

A really basic rule of thumb - can you understand the whole thing as well or better on one line? Ternary is OK. Otherwise expand it.

๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ how to use the ternary operator in javascript
How to use the ternary operator in JavaScript - LogRocket Blog
February 21, 2025 - An understanding of how to use the comparison operators: === (strict equality) !== (strict inequality) > (greater than) < (less than) >= (greater than or equal) <= (less than or equal) The goal of this article is to add to your JavaScript knowledge of shortcuts by mastering the ternary operator.
๐ŸŒ
YouTube
youtube.com โ€บ bro code
JavaScript TERNARY OPERATOR in 6 minutes! โ“ - YouTube
00:00:00 intro00:00:32 example 100:02:37 example 200:03:36 example 300:04:30 example 4
Published ย  October 23, 2023
Views ย  20K
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.

๐ŸŒ
Alma Better
almabetter.com โ€บ bytes โ€บ tutorials โ€บ javascript โ€บ ternary-operator-in-javascript
Ternary Operator in JavaScript
June 22, 2023 - In summary, the Ternary Operator is a shorthand way of writing conditional statements in JavaScript. It allows us to evaluate a condition and return one of two possible values based on whether the condition is true or false.
๐ŸŒ
Rithm School
rithmschool.com โ€บ using-the-javascript-ternary-operator
Mastering the JavaScript Ternary Operator: Effective Usage
May 22, 2023 - If that statement is a truthy expression, then the first statement (after the ? but before the :) will be the result of the ternary operation. If the conditional statement is falsy, then the second statement (after the :) will be the result of the ternary operation. In the case of our example, personType will be assigned the string "adult".
๐ŸŒ
W3Schools
w3schools.com โ€บ react โ€บ react_es6_ternary.asp
React ES6 Ternary Operator
The ternary operator is a simplified conditional operator like if / else. Syntax: condition ? <expression if true> : <expression if false> ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an ...
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Ternary_conditional_operator
Ternary conditional operator - Wikipedia
1 month 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...
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ curriculum help
Ternary operator - Curriculum Help - The freeCodeCamp Forum
April 14, 2025 - In the article How to Build a Responsive Form with Filter Functionality Using HTML, CSS, and JavaScript Iโ€™m having trouble converting the ternary operator back to a standard if/else statement. It would help me understandโ€ฆ
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ javascript โ€บ javascript ternary operators: a step-by-step guide
How to Use Ternary Operators in JavaScript | Career Karma
December 1, 2023 - The JavaScript ternary operator evaluates a statement and returns one of two values. A ternary operator can be instructed to return a value if the statement evaluates to either true or false. The syntax for the ternary operator is: statement ?
๐ŸŒ
YouTube
youtube.com โ€บ watch
Ternary Operator in Javascript | Tutorial for Beginners - YouTube
Web Dev Roadmap for Beginners (Free!): https://bit.ly/DaveGrayWebDevRoadmapIn this video we'll work through examples of Ternary Operators in Javascript. All ...
Published ย  August 25, 2020