loading

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:

Example

Before:

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

Here’s the same example with a ternary operator.

Example

With Ternary

				
					authenticated ? renderApp() : renderLogin();
				
			
Share this Doc

ES6 Ternary Operator

Or copy link

Explore Topic