Tutorials References Exercises Videos Menu
Paid Courses Website NEW Pro NEW

React ES6 Ternary Operator


Ternary Operator

The ternary operator is a simplified conditional operator like if / else.

Syntax: condition ? <expression if true> : <expression if false>

Here is an example using if / else:

Before:

if (authenticated) {
  renderApp();
} else {
  renderLogin();
}

Try it Yourself »

Here is the same example using a ternary operator:

With Ternary

authenticated ? renderApp() : renderLogin();

Try it Yourself »


Test Yourself With Exercises

Exercise:

Complete this ternary operator statement.

blue  renderBlue()  renderRed();

Start the Exercise