JavaScript Conditional Ternary Operator

๐Ÿ“˜ What is the Ternary Operator?

The ternary operator is a shortcut for if-else statements. It has the form:

condition ? expressionIfTrue : expressionIfFalse
            

๐Ÿ’ป Examples

let age = 18;
let status = (age >= 18) ? 'Adult' : 'Minor';
console.log(status); // 'Adult'

let score = 75;
let result = (score >= 50) ? 'Pass' : 'Fail';
console.log(result); // 'Pass'
            

๐Ÿงช Try It Yourself

Click the button to see the ternary operator in action: