Yes, it's valid, and it runs fine in Chrome:
var a, b, c;
a = 6;
b = 7;
c = a !== b ? (a = 1, b = 2) : (a = 2, b = 1);
console.log("a = " + a);
console.log("b = " + b);
console.log("c = " + c);
I'm not saying it's a remotely good idea in code humans are meant to read. :-) I expect jamietre is correct in the comments when he/she says it looks like the result of minification.
The comma operator is a binary operator (an operator accepting two operands). It evaluates its left-hand operand (thus causing any side-effects it has, such as assignment), throws that result away, then evalutes its right-hand operand (thus causing its side-effects if any) and takes that result as its result value. If you have multiple comma operators in a row, the overall expression is evaluated in order, left-to-right, with the final result being the value resulting from the right-most operand evaluation.
And of course, you know the conditional operator (a ternary operator — one accepting three operands) is used to pick one of two sub-expressions to evaluate, on the basis of an initial expression.
So that line is very...expressive...what with a total of seven* different expressions inside it.
So in that example, the result of the overall expression is 2 if a !== b initially, or 1 if a === b initially, with the side-effects of setting a and b.
It's the side effects that make it, in my view, a questionable choice. And of course, there's no reason to use the comma operator if the left-hand operand doesn't have side effects.
* Yes, seven of 'em packed into that overall ternary:
a !== b- the first comma expression
a = 1b = 2- the second comma expression
a = 2b = 1
Re your edit with the actual statement, that one works too:
function test(a) {
var b = 7,
d = 1,
e = 2,
f = 3,
g = 4,
h = 5,
i = 6;
a!==0?b<0?(h=b/a,e=h-1,f=-2*b+2*a*e,i=-2*b+2*a*h,d=2*h*a-2*b-2*a):(h=b/a,e=h+1,f=2*b-2*a*e,i=2*b-2*a*h,d=-2*h*a+2*b):d=h=e=f=i=0;
console.log("a = " + a);
console.log("b = " + b);
console.log("d = " + d);
console.log("e = " + e);
console.log("f = " + f);
console.log("g = " + g);
console.log("h = " + h);
console.log("i = " + i);
}
test(0);
test(1);
.as-console-wrapper {
max-height: 100% !important;
}
But wow, I hope this is minified, because if a person wrote that, they must really have a thing against anyone who's supposed to maintain it later... ;-)
Answer from T.J. Crowder on Stack OverflowYes, it's valid, and it runs fine in Chrome:
var a, b, c;
a = 6;
b = 7;
c = a !== b ? (a = 1, b = 2) : (a = 2, b = 1);
console.log("a = " + a);
console.log("b = " + b);
console.log("c = " + c);
I'm not saying it's a remotely good idea in code humans are meant to read. :-) I expect jamietre is correct in the comments when he/she says it looks like the result of minification.
The comma operator is a binary operator (an operator accepting two operands). It evaluates its left-hand operand (thus causing any side-effects it has, such as assignment), throws that result away, then evalutes its right-hand operand (thus causing its side-effects if any) and takes that result as its result value. If you have multiple comma operators in a row, the overall expression is evaluated in order, left-to-right, with the final result being the value resulting from the right-most operand evaluation.
And of course, you know the conditional operator (a ternary operator — one accepting three operands) is used to pick one of two sub-expressions to evaluate, on the basis of an initial expression.
So that line is very...expressive...what with a total of seven* different expressions inside it.
So in that example, the result of the overall expression is 2 if a !== b initially, or 1 if a === b initially, with the side-effects of setting a and b.
It's the side effects that make it, in my view, a questionable choice. And of course, there's no reason to use the comma operator if the left-hand operand doesn't have side effects.
* Yes, seven of 'em packed into that overall ternary:
a !== b- the first comma expression
a = 1b = 2- the second comma expression
a = 2b = 1
Re your edit with the actual statement, that one works too:
function test(a) {
var b = 7,
d = 1,
e = 2,
f = 3,
g = 4,
h = 5,
i = 6;
a!==0?b<0?(h=b/a,e=h-1,f=-2*b+2*a*e,i=-2*b+2*a*h,d=2*h*a-2*b-2*a):(h=b/a,e=h+1,f=2*b-2*a*e,i=2*b-2*a*h,d=-2*h*a+2*b):d=h=e=f=i=0;
console.log("a = " + a);
console.log("b = " + b);
console.log("d = " + d);
console.log("e = " + e);
console.log("f = " + f);
console.log("g = " + g);
console.log("h = " + h);
console.log("i = " + i);
}
test(0);
test(1);
.as-console-wrapper {
max-height: 100% !important;
}
But wow, I hope this is minified, because if a person wrote that, they must really have a thing against anyone who's supposed to maintain it later... ;-)
Yes:
a=1;
b=2;
a!==b ? (a=1, b=2) : (a=2, b=1)
console.log(a); // 1
console.log(b); // 2
and:
a=1;
b=2;
a===b ? (a=1, b=2) : (a=2, b=1)
console.log(a); // 2
console.log(b); // 1
As you can analyze, changing the equality operator reacts correctly to our test if you look at the results.
You could do the following:
<div style={{'backgroundColor': status === 'approved' ? 'blue' : status === 'pending' ? 'black' : 'red'}}>
</div>
This means if status === 'approved' set the background color as blue, if status === 'pending' set it as black, else set it as red.
I would suggest using functions if your conditions get complicated, to not degrade your code readability.
getBackgroundColor(status) {
if (status === 'approved') {
return 'blue';
}
if (status === 'pending') {
return 'red';
}
return 'black';
}
render() {
// ...
return (
<div style={{ 'backgroundColor': this.getBackgroundColor(status) }}></div>
);
}
So I saw an article recently that was talking about minimizing the use of ternary operators where possible and reflecting on my own use of them especially in JSX, I have a few questions...
Before I get decided to post my questions, I checked React subs and most discussions on this are a couple years old at least and I thought perhaps views have changed.
Questions:
Is the main issue with using nested ternary operators readability?
I have found myself using ternary operators more and more lately and I even have my own way of formatting them to make them more readable. For example,
info.type === "playlist"
? info.creationDate
? <div className="lt-info-stats">
<span className="text pure">Created on {info.creationDate}</span>
</div>
: null
: info.type === "artist"
? <div className="lt-info-stats">
<span className="text pure">{info.genre}</span>
</div>
: <div className="lt-info-stats">
<span className="text pure">{info.releaseDate}</span>
<span className="cdot" style={{ fontWeight: "bold", margin: "1px" }}>·</span>
<span className="text pure">{info.genre}</span>
</div>When written like this, I can visually see the blocks and tell them apart and it looks a lot like how an if/else might look.
nested ternary operator formatting2. What is the preferred formatting of ternary operators in general and what do you think should be done to make them more readable?
3. How do people feel about nested ternary operators today? How big of a nono is it to have them in code (if it is a nono)?
I would love you know peoples thoughts on ternary operators in React in general as well.
Thanks for your attention!
Specifically, the problem is that the ternary operator resolves into something that is then evaluated by whatever is on the left of the ternary.
For example
const x = condition
? result1
: result2;
but if there's nothing on the left of the ternary, it doesn't do anything except side effects, which are done much more clearly with if statements. (you should also probably use if/else if your code is more complicated than can reasonably fit on a single line)
Maybe you wanted
if (!auth.isAuthenticated()) return (
<whateverComp auth={auth} {...props} />
);
sessionStorage.setItem('page_name', 'dashboard');
return (
<Redirect to="/log-in"/>
);
choreDoor === 0 ?
(openDoor1 = botDoorPath,
openDoor2 = beachDoorPath,
openDoor3 = spaceDoorPath)
: choreDoor === 1 ?
(openDoor2 = botDoorPath,
openDoor1 = beachDoorPath,
openDoor3 = spaceDoorPath)
: choreDoor === 2 ?
(openDoor3 = botDoorPath,
openDoor1 = beachDoorPath,
openDoor2 = spaceDoorPath)
: false;
Solved my issue
try this,
return (
<View>
{item.diaryClass.name === "pack" && item.diaryId === 778 ?
(<Text>
chicken
</Text>)
:
(null)
}
{item.diaryClass.name === "pack" && item.diaryId === 776 ?
(<Text>
buger
</Text>)
:
(null)
}
{item.diaryClass.name === "pack" && item.diaryId === 775 ?
(<Text>
pizza
</Text>)
:
(null)
}
</View>
)
Use this code:
return (
{item.diaryClass.name === "pack" && item.diaryId === 778 ?
(<Text>
chicken
</Text>)
:
item.diaryClass.name === "pack" && item.diaryId === 776 ?
(<Text>
buger
</Text>)
:
item.diaryClass.name === "pack" && item.diaryId === 775 ?
(<Text>
pizza
</Text>)
:
(null)
}
<h1 id="largeh1">
{
isEnvironmentBFE
? (
outputEnFr(
"BFE Environment English Text",
"BFE Environment French Text",
this.props.lang
)
) : NEW CONDITIONAL HERE
? (NEW RESULT)
: (
outputEnFr(
"Acme Environment English Text",
"Acme Environment French Text",
this.props.lang
)
)
}
</h1>
This is equivalent to
if(isEnvironmentBFE)
//yadda
else if (foo)
//yadda yadda
else
//yadda
The most readable way to go about this is by moving that logic out of your return statement and creating a function for it which handles that logic. You can do this within the same component, or extract that function to a different file if it should be reusable on another component:
const Component = () => {
const renderText = () => {
if (...) {
return ...
} else if (...) {
return ...
}
return ...
}
return (
<h1>
renderText();
<h1>
)
};
If you would like to stick with a ternary you can do:
return condition1 ? (condition2 ? result1 : result2) : result3
Depends on your personal preference, although I suggest using an if-statement or a switch-statement for anything that has more than 1 condition as it is easier to read and comprehend what is happening.
You could return an array of components:
{
views === "monthly"
? [this.renderDays(), this.renderCells()]
: null
}
Or if the methods return arrays itself, just spread them:
{
views === "monthly"
? [...this.renderDays(), ...this.renderCells()]
: null
}
You can use a comma to separate expressions and wrap that in a single statement with parentheses for the ternary.
{ views === "monthly" ? (this.renderDays(), this.renderCells()): null }
Reference: How to I execute multiple functions on the result of a ternary operation?