🌐
Programiz
programiz.com › javascript › ternary-operator
JavaScript Ternary Operator (with Examples)
The ternary operator takes three operands, hence, the name ternary operator. It is also known as a conditional operator. Let's write a program to determine if a student passed or failed in the exam based on marks obtained.
🌐
Built In
builtin.com › software-engineering-perspectives › ternary-operator-js
Ternary Operator in JavaScript Explained | Built In
July 13, 2023 - 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.
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
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
Ternary operator
Personally I don't mind this too much, in the context it is presented. It reads clearly enough to me. A function with early returns is an option function grade(mark){ if(mark > 79) return "A"; if(mark > 69) return "B" // etc return "F"; } let myGrade = grade(72); Although I disagree with some of the other views - this isn't inherently more readable to my eyes. Having said that, as a function it is reusable and you can more easily change the inners of it if you need to account for more complex logic. More on reddit.com
🌐 r/learnjavascript
84
122
May 23, 2022
Is using the ternary operator bad practice?
If you don't abuse the ternary operator as if replacement and if there is no nesting, it is perfectly fine to use the ternary operator. Actually, it is far more elegant to write int y = (x > 0) ? x : x * (-1); Than int y; if (x > 0) { y = x; } else { y = x * (-1); } Nested ternary operators are a no-go. More on reddit.com
🌐 r/learnprogramming
11
1
February 2, 2022
🌐
freeCodeCamp
freecodecamp.org › news › why-a-ternary-operator-is-not-a-conditional-operator-in-js
Why a Ternary Operator is not a Conditional Operator in JS
January 17, 2023 - The reason why many people (including myself, until recently) call the ternary operator a conditional operator, is because the conditional operator is the only ternary operator in JavaScript (and some other languages as well).
🌐
SitePoint
sitepoint.com › blog › javascript › quick tip: how to use the ternary operator in javascript
Quick Tip: How to Use the Ternary Operator in JavaScript — SitePoint
November 6, 2024 - The ternary operator in JavaScript is a shorthand way of writing an if-else statement. It is called the ternary operator because it takes three operands: a condition, a result for true, and a result for false.
🌐
W3Schools
w3schools.com › java › java_conditions_shorthand.asp
Java Short Hand If...Else (Ternary Operator)
Tip: Use the ternary operator for short, simple choices. For longer or more complex logic, the regular if...else is easier to read. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript ...
🌐
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.
🌐
Scaler
scaler.com › home › topics › javascript › javascript ternary operator
JavaScript Ternary Operator - Scaler Topics
February 19, 2024 - JavaScript's Ternary Operator (?:) condenses conditional logic with three operands, offering an alternative to if-else statements. Enhancing code readability and simplicity, it enables concise representation of conditional blocks within expressions.
🌐
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
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 ...
🌐
CodingNomads
codingnomads.com › javascript-ternary-operator
The JavaScript Ternary Operator
Learn when and how to use the ternary operator in JavaScript, a concise way of writing conditional statements.
🌐
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.
🌐
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.
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.

🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
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');
🌐
React
react.dev › learn › conditional-rendering
Conditional Rendering – React
You’d have to do it in two places in your code! In such a situation, you could conditionally include a little JSX to make your code more DRY. JavaScript has a compact syntax for writing a conditional expression — the conditional operator or “ternary operator”.
🌐
DEV Community
dev.to › sahilthakur7 › ternary-operator-in-javascript-with-examples-4f2i
Ternary operator in Javascript with examples - DEV Community
June 16, 2020 - In this article we’ll see how to use and more importantly how now to use the ternary operator in Javascript with examples.