🌐
W3Schools
w3schools.com › js › js_if_ternary.asp
JavaScript Conditional Ternary Operator
The conditional (ternary) operator is the only JavaScript operator that takes three operands. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.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 › 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 ...
🌐
W3Schools
w3schools.com › js › js_comparisons.asp
JavaScript Comparison Operators
All the comparison operators above can also be used on strings: let text1 = "A"; let text2 = "B"; let result = text1 < text2; Try it Yourself » ... Comparing data of different types may give unexpected results. When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison.
🌐
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   June 24, 2019
🌐
W3Schools
w3schools.com › js › exercise.asp
Exercise v1.3
Use a conditional (ternary) operator for this exercise: If the variable firstName is equal to "John", the value of the variable result should be "Hello John!", otherwise the value of result should be "You're not John!" · Syntax hint: (condition) ? value1:value2;
🌐
Programiz
programiz.com › javascript › ternary-operator
JavaScript Ternary Operator (with Examples)
In this tutorial, you will learn about the conditional/ternary operator in JavaScript with the help of examples.
🌐
W3Schools
w3schools.com › js › exercise_js.asp
W3Schools JS Exercise
Choose the correct conditional (ternary) operator to alert "Too young" if age is less than 18, otherwise alert "Old enough" · Click here to try again
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript ternary operator
JavaScript Ternary Operator
November 15, 2024 - This tutorial shows you how to use the JavaScript ternary operator, a shortcut of the if-else statement, to clean up your code.
Find elsewhere
🌐
W3Schools
w3schoolsua.github.io › js › js_comparisons_en.html
JavaScript Comparison. Examples. Lessons for beginners. W3Schools in English
All the comparison operators above can also be used on strings: let text1 = "A"; let text2 = "B"; let result = text1 < text2; Try it Yourself » ... Comparing data of different types may give unexpected results. When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison.
🌐
freeCodeCamp
freecodecamp.org › news › the-ternary-operator-in-javascript
JavaScript Ternary Operator – Syntax and Example Use Case
January 6, 2023 - You cannot use the operator without assigning the returned value to a variable: const result = condition ? trueExpression : falseExpression · The returned value depends on the evaluation of the condition expression. If the condition is true, the returned value returned from trueExpression is assigned to the variable. Else, the returned value from falseExpression will be assigned to the variable. The ternary operator can be a good replacement for if statements in some cases.
🌐
W3Schools
w3schools.com › js › js_operators.asp
JavaScript Operators
JS Operators JS Arithmetic JS Assignment JS Comparisons JS If Conditions · JS Conditional JS If JS If Else JS Ternary JS Switch JS Booleans JS Logical JS Loops · JS Loops JS Loop for JS Loop while JS Break JS Continue JS Control Flow JS Strings · JS Strings JS String Templates JS String Methods JS String Search JS String Reference JS Numbers ·
🌐
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.
🌐
Mimo
mimo.org › glossary › javascript › ternary-operator
JavaScript Ternary Operator: Syntax, Usage, and Examples
Write cleaner conditional logic in JavaScript using the ternary operator. Replace if...else with concise, readable expressions in a single line.
🌐
W3Schools
w3schoolsua.github.io › react › react_es6_ternary_en.html
React ES6 Ternary Operator. Lessons for beginners. W3Schools in English
React ES6 Ternary Operator. The ternary operator is a simplified conditional operator like if / else. Examples. Lessons for beginners. W3Schools in English
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Expressions_and_operators
Expressions and operators - JavaScript | MDN
This chapter describes JavaScript's expressions and operators, including assignment, comparison, arithmetic, bitwise, logical, string, ternary and more.
🌐
W3Schools
w3schools.com › js › tryit.asp
W3Schools Tryit Editor - JavaScript Comparison
The W3Schools online code editor allows you to edit code and view the result in your browser
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.

🌐
W3Schools
w3schools.com › js › js_if_else.asp
JavaScript else Statement
JS Operators JS Arithmetic JS Assignment JS Comparisons JS If Conditions · JS Conditional JS If JS If Else JS Ternary JS Switch JS Booleans JS Logical JS Loops · JS Loops JS Loop for JS Loop while JS Break JS Continue JS Control Flow JS Strings · JS Strings JS String Templates JS String Methods JS String Search JS String Reference JS Numbers ·
🌐
freeCodeCamp
freecodecamp.org › news › javascript-ternary-operator-explained
How to Use the Ternary Operator in JavaScript – Explained with Examples
February 27, 2024 - If the age is 18 or older, the expression becomes true, so the operator returns true after the ?. Otherwise, it returns false. This result is saved in a variable and then returned from the function. While this basic use case simplifies code and improves readability by replacing unnecessary if-else blocks, it's important to use it sparingly to avoid cluttering and complicating your code. Later, we'll discuss best practices for using the ternary operator.