JavaScript Tip: Ternary Operator

Using many if or switch statements can make your code very long and complex in no time. A trick to avoid some of the bloating involves using something called the ternary operator.

The ternary operator syntax is:

This is very handy for boolean conditions or very short values.

For example, you can replace this long if condition with one line of code:

Leave a Reply

Your email address will not be published. Required fields are marked *