Skip to main content

Command Palette

Search for a command to run...

Use Ternary Operators - Daily JavaScript Tips #2

Published
1 min read
Use Ternary Operators - Daily JavaScript Tips #2
D

I am 17 years old and a young passionate and self-taught frontend web developer and have an intention to become a successful developer. I usually write about JavaScript and Web Development and share some tips in the articles.

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";
}

Ternary operators

let fruit;
let ternary = (fruit === "Apple") ? "It is a fruit" : "It is not a fruit";

See how it got executed in one line


Thank you for reading

More from this blog

D

Dhairya Shah

119 posts