javascript / beginner
Snippet
The Ternary Operator for Quick Decisions
The ternary operator is a one-line alternative to an if-else statement. it asks a question (condition), followed by a '?' for the true result and a ':' for the false result.
snippet.js
javascript
1
2
3
4
const score = 85;const result = score >= 50 ? "Passed" : "Failed";console.log(result); // Outputs: "Passed"
nextjs
Breakdown
1
score >= 50
The condition to check.
2
? "Passed" : "Failed"
Returns 'Passed' if true, otherwise returns 'Failed'.