JavaScript Beginner Basics · 1 min read

JavaScript ternary operator

Start the JavaScript learning path »

❮ JavaScript Home All Tutorials ❯

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

condition ? a : b.

Example

Interactive preview available
JavaScript Example
const age = 18;
const label = age >= 18 ? 'Adult' : 'Minor';

Line-by-Line Walkthrough

Keep ternaries short; nest sparingly.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…