I was trying to understand ternary operator into normal if else condition.
JavaScript
x
7
1
stateable =
2
data.length > 0
3
? data[0].list > data[0].somelist
4
? Product.ADDING
5
: Product.REMOVING
6
: Product.NOTHING
7
how to understand this multiple nested condition
Advertisement
Answer
It’s equivalent to the following:
JavaScript
1
9
1
let stateable;
2
if (data.length === 0) {
3
statable = Product.NOTHING;
4
} else if (data[0].list > data[0].somelist) {
5
statable = Product.ADDING;
6
} else {
7
statable = Product.REMOVING;
8
}
9
The conditional operator approach could be refactored to be more readable by putting the NOTHING condition first, though, if you wanted:
JavaScript
1
6
1
const stateable = data.length === 0
2
? Product.NOTHING
3
: data[0].list > data[0].somelist
4
? Product.ADDING
5
: Product.REMOVING;
6