0 day streak
javascript / beginner
Snippet

The Ternary Operator

The ternary operator is the only JavaScript operator that takes three operands: a condition followed by a question mark, then an expression to execute if the condition is truthy, and finally a colon followed by an expression if the condition is falsy.

snippet.js
javascript
1
2
3
4
const age = 20;
const status = age >= 18 ? 'Adult' : 'Minor';
console.log(status);
nodejs
Breakdown
1
const status = age >= 18 ? 'Adult' : 'Minor';
Checks if age is 18 or more. If true, returns 'Adult'; otherwise, returns 'Minor'.
2
console.log(status);
Outputs the result of the conditional check to the console.