DEV Community

Komal Shehzadi
Komal Shehzadi

Posted on

JS- Ternary Operator

TL;DR The ternary operator is a shortcut for if/else

It contains three operands

A condition
A question mark ?
A colon :

if/else

let fruit;
if(fruit === "Apple"){
    return "It is a fruit";
}else{
    return "It is not a fruit";
}
Enter fullscreen mode Exit fullscreen mode

Ternary operators

let fruit;
let ternary = (fruit === "Apple") ? "It is a fruit" : "It is not a fruit";
Enter fullscreen mode Exit fullscreen mode

See how it got executed in one line

Top comments (0)