Open In App

What are the Benefits of Using a Ternary Operator ?

The ternary operator, often denoted by the ? : syntax, is a concise way to write conditional expressions in various programming languages.

Here are some benefits of using the ternary operator:



Example: Here, the condition isSunny is checked, and the weatherMessage variable is assigned a value based on whether it’s sunny or not. The equivalent code using the ternary operator accomplishes the same result more concisely.




let isSunny = true;
let weatherMessage = isSunny ? 'It is sunny today!' : 'It is not sunny today.';
console.log(weatherMessage);

Output

It is sunny today!
Article Tags :