← CodePrimer
JavaScript
Beginner
Basics
JavaScript ternary operator
How do I write a one-line if/else?
condition ? a : b.
const age = 18;
const label = age >= 18 ? 'Adult' : 'Minor';
Notes
Keep ternaries short; nest sparingly.
Keywords: ternary · conditional · operator
Related
HTML
HTML headings h1–h6
How do I create page headings?
CSS
Three ways to apply CSS
What are inline, internal, and external CSS?
JavaScript
document.getElementById
How do I select one element by id?
PHP
Embed PHP inside HTML
How do I mix PHP and HTML?
HTML
Paragraphs and line breaks
How do I break text into paragraphs?
CSS
CSS box model
Why is my element wider than expected?