javascript / beginner
Snippet
Conditional Logic with the Ternary Operator
The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns the first value if true, or the second value if false.
snippet.js
1
2
3
4
const score = 85;const grade = score >= 50 ? 'Pass' : 'Fail';console.log(grade);
nodejs
Breakdown
1
score >= 50
The condition to check.
2
? 'Pass'
The value returned if the condition is true.
3
: 'Fail'
The value returned if the condition is false.