Summary
js
true ? 1 : 0;
// output: 1
false ? 1 : 0;
// output: 0
Concept
Ternary operator has 3 parameters/operands:
- Predicate
- Consequent
- Alternative
Based on the boolean predicated, either the consequent or alternative is returned
Application
Absolute function
js
function abs(x) {
return x >= 0 ? x : -x;
}
abs(5);
// output: 5
abs(-8);
// output: 8
Binary boolean operators
js
a || b // a or b
// is equivalent to
a ? true : b // if a, return true, else return b
a && b // a and b
// is equivalent to
a ? b : false // if a, return b, else return false