← CodePrimer
JavaScript Beginner Basics

JavaScript ternary operator

How do I write a one-line if/else?

condition ? a : b.

Sample
const age = 18;
const label = age >= 18 ? 'Adult' : 'Minor';

Notes

Keep ternaries short; nest sparingly.

Keywords: ternary · conditional · operator

Related

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share