DEV Community

Michael Fazekas
Michael Fazekas

Posted on

Ternary Operator

Hey Everyone!
SO as the title suggest this post will be about Ternary Operator in JavaScript.

Why You ask?

Well I find myself on this long journey of learning JavaScript and ES6+, I really want to share my knowledge of the basics and hopefully this can be some what resourceful to not only myself but someone down the road.

What is a Ternary Operator?

Ternary Operator is like a short-hand method of if/else statement.
Image description

What's so Great about it?

instead of having to code out something like this:
Function user(){
if(something){
//Code that does something
}else{
// code that does something else.
}
}

you can just use a ternary Operator.
Example of a ternary Op.
Condition ? expression 1 : expression 2;

Some Good things NOT to do with Ternary OP.

Image description

  • Nest the operator makes it harder to read
//example
condition
?
expression 1 :
expression 2
Enter fullscreen mode Exit fullscreen mode

DON'T DO IT! I will hunt you down! (only kidding jeez)

  • using it on complex problems (chances are it wont work anyways but be mindful.)

Top comments (0)