JavaScriptEasy
What is the ternary operator and how is it used?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
The ternary operator is a shorthand for an if-else statement in JavaScript. It takes three operands: a condition, a result for true, and a result for false. The syntax is condition ? expr1 : expr2. For example, let result = (a > b) ? 'a is greater' : 'b is greater'; assigns 'a is greater' to result if a is greater than b, otherwise it assigns 'b is greater'.
