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 Overflowjquery - JavaScript ternary operator example with functions - Stack Overflow
Javascript ternary operator syntax with object
Ternary operator - JavaScript - SitePoint Forums | Web Development & Design Community
Question on if/when to use Ternary Operators?
Videos
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!)
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.
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
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');Pro: Readability
While plenty of people talk about the readability problems with misused ternary operators, there are also situations where using a full if/else would be annoyingly bulky, spreading a simple statement across five lines, and significantly reducing readability. Here are some sample statements pulled from a project I'm working on:
bg_2d.fillStyle = is_in_bounds(x, y) ? grid_colors[x][y] : "#ffffff";
const diagonal_slowdown = is_diagonal() ? Math.SQRT2 : 1;
const color = (team == "red") ? "#ff0000" : "#0000ff";
When you have a simple situation where you have two things to pick from based on a condition, which happens all the time, ternary is perfect. Having a short syntax for ternary massively improves readability in these situations.
Pros:
It's an expression, and in languages with distinction between expressions and statements it's crucial to have a selection operator available as an expression. Ternary operator is not necessarily the best syntax though, as the other answers highlighted how convoluted it can become with very little effort. A good alternative is an if-then-else available as an expression and not just statement.
Cons:
It's introducing control flow into expressions, just like logic operators in many languages. When side effects are possible, it's making expressions less visually obvious to follow.
The C-style syntax of C?T:F often leads to convoluted and error-prone code.