javascript / beginner
Snippet
The Ternary Operator for UI Logic
The ternary operator is a concise way to write an if-else statement. It evaluates a condition and returns one of two values, which is very helpful for setting component styles or text in Next.js.
snippet.js
1
const status = isActive ? 'Online' : 'Offline';
nextjs
Breakdown
1
isActive ?
The boolean condition being tested.
2
'Online'
The value returned if the condition is true.
3
: 'Offline'
The value returned if the condition is false.