The conditional operator is a one liner if/else statement. Another name for it is ternary operator because it uses three operands:
expression1 ? expression2 : expression3;
If expression1 evaluates to boolean true, expression2 gets executed. Else, expression3 gets executed instead.
If you’re like me, you probably prefer to use if, else if, else statements, but the ternary operator can be very handy at times. One of the things we can’t do with our regular branching techniques is to include alternative outputs on a one console.log statement, but with the ternary operator we can:
var x = 10; var y = 9; console.log(x < y ? "hello!" : "goodbye!"); // it outputs "goodbye!" console.log(x > y ? "hello!" : "goodbye!"); // it outputs "hello!"