javascript / beginner
Snippet
Conditional Assignment with the Ternary Operator
The ternary operator is the only JavaScript operator that takes three operands. It serves as a compact alternative to if-else statements when you need to assign a value based on a condition.
snippet.js
1
2
3
4
const score = 85;const result = score >= 50 ? 'Pass' : 'Fail';console.log(result); // "Pass"
nodejs
Breakdown
1
score >= 50
The condition to evaluate (true or false).
2
? 'Pass'
The value to return if the condition is true.
3
: 'Fail'
The value to return if the condition is false.