JavaScript ternary operator is short form of writing if and else statement in JavaScript. To understand how to use ternary operator in JavaScript, let’s have a look at some conditional JavaScript code example.
let status = true;
if(status){
console.log("Status is true.");
} else {
console.log("Status is false.");
}
Now let’s write the above code example using ternary operator in JavaScript.
let status = false;
status ? console.log("Status is true.") : console.log("Status is false.");
In JavaScript ternary operator we use two symbols, ? (question mark) to denote if the condition is true and : (colon) to denote if the condition is not true.
From a personal point of view, I normally tend to avoid ternary operator. It makes the code difficult to read compared to a normal if else statement. But who knows, you may like it ;)