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!)

Answer from JonnyReeves on Stack Overflow
🌐
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.
🌐
Refine
refine.dev › home › blog › tutorials › how to use the javascript ternary operator
How to Use the JavaScript Ternary Operator | Refine
October 8, 2024 - 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.
Discussions

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
Javascript ternary operator syntax with object
The ternary operator needs to have an else clause that tells it what to do if the condition is false. More on reddit.com
🌐 r/learnprogramming
10
4
September 9, 2023
Ternary operator - JavaScript - SitePoint Forums | Web Development & Design Community
It’s well know that the ternary operator sintax is something like so: (test) ? true doThis : false doThat Suppose that in case condition is false we want do nothing. How to code “do nothing”? May I let it blank or there are an appropriate sintax? More on sitepoint.com
🌐 sitepoint.com
0
August 22, 2010
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
🌐
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.
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.

🌐
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
🌐
Reddit
reddit.com › r/learnprogramming › javascript ternary operator syntax with object
r/learnprogramming on Reddit: Javascript ternary operator syntax with object
September 9, 2023 -

Hello, i got some issues with the syntax for this line of code:

counter === null ? counter = {wins: 0, losses:0, ties:0};

It tells me there is some issue with the ';'. I never had such a problem with ternary operators before, it seems like to be related with the object inside of it. what would be the right declaration of it?

Thanks

Find elsewhere
🌐
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 ...
🌐
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.
🌐
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.
🌐
Built In
builtin.com › software-engineering-perspectives › ternary-operator-js
Ternary Operator in JavaScript Explained | Built In
The ternary operator gets its name by being the only operator in JavaScript that takes three operands, or parts. The first part of a ternary operation is a logical condition that returns a true or false value.
🌐
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 Code Challenges 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.
🌐
DEV Community
dev.to › megdiv › javascript-ternary-operator-kmo
JavaScript Ternary Operator - DEV Community
November 8, 2025 - You are also able to return with a ternary operator by listing "return" before your condition: ... Unpacking this statement, it should read: Is the item a circle? If the item is a circle, return true If the item is not a circle, return false · By using "return" with a ternary you must always return from either condition. It's not able to be used in instances where you only want to return on true, but not false, and vice versa. All in all, I hope this helped understand the JavaScript Ternary Operator a bit better and why I find it so visually clean and fun to use.
🌐
SitePoint
sitepoint.com › javascript
Ternary operator - JavaScript - SitePoint Forums | Web Development & Design Community
August 22, 2010 - It is to simplify the assignment of a value to someVariable that you use a ternary operator and so there is no “do nothing” case since the purpose of a ternary operator is to select which of two values to assign.
🌐
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.
🌐
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');
🌐
Medium
medium.com › @trig79 › javascript-the-early-lessons-ternary-operator-made-of-3-parts-or-is-it-1103f855964f
JavaScript The Early Lessons: Ternary Operator — Made of 3 Parts…Or Is It? | by Trig | Medium
April 7, 2022 - Unlike the dictionary definition, we can chain together the ternary operator and create a ternary made of more than 3 parts…mind blown!!!
🌐
React
react.dev › learn › conditional-rendering
Conditional Rendering – React
JavaScript has a compact syntax for writing a conditional expression — the conditional operator or “ternary operator”.
🌐
Medium
medium.com › @justintulk › ternary-operators-and-coercing-truthy-falsy-if-statements-1e9296019fb
Ternary Operators and Coercing Truthy/Falsy if() Statements | by Justin Tulk | Medium
September 29, 2015 - You can also put one ternary operator inside of another one, but the space you save will probably be more than offset by how long it will take you to reason about what you expect back out. Another thing I see littered around is using an if() statement on an expression that doesn’t seem readily equivalent to either a true or false value. Like: ... What’s happening here is that Javascript can coerce the result of the expression in an if() statement to be a boolean value, i.e.
🌐
DEV Community
dev.to › nicm42 › ternary-operators-in-javascript-go5
Ternary operators in JavaScript - DEV Community
December 12, 2021 - Ternary operators are also known as conditional operators. Basically, they put an if else statement... Tagged with javascript.