A ternary operator is a short way to write an if...else statement. It checks a condition and returns one of two values depending on whether the condition is true or false.

Syntax:

condition ? expressionIfTrue : expressionIfFalse;

? — if the condition is true
: — separates the true and false results

JavaScript:

let age = 20;

let result = age >= 18 ? "Eligible" : "Not Eligible";

console.log(result);

Output:

Eligible.

Difference between if...else and ternary:

let mark = 80;

if (mark >= 35) {
console.log("Pass");
} else {
console.log("Fail");
}

Using ternary operator:

let mark = 80;

console.log(mark >= 35 ? "Pass" : "Fail");

Output for both:

Pass.