condition ? a : b is an expression that gives a when the condition is true and b otherwise. It is if/else for values: const label = n % 2 === 0 ? "even" : "odd";. Use it for simple choices; nested ternaries are hard to read.
Every value is truthy or falsy when used as a condition. The falsy ones are false, 0, "" (empty string), null, undefined, and NaN; everything else is truthy, including "0" and empty arrays. || returns the first truthy operand, which gives defaults: name || "Anonymous". ?? is stricter: it only falls back for null and undefined, so 0 and "" survive.
const items = [];
console.log(items.length ? "has items" : "empty"); // empty
console.log(0 || 10, 0 ?? 10); // 10 0Your task
Write label(n) returning "even" or "odd" with a ternary, displayName(name) returning the name or "Anonymous" when the name is empty or missing, and count(value) returning value when it is a number (including 0) and 0 when it is null or undefined.
Hint 1
return n % 2 === 0 ? "even" : "odd";
Hint 2
name || "Anonymous" treats "" as missing; value ?? 0 keeps 0.
Show the solution
function label(n) {
return n % 2 === 0 ? "even" : "odd";
}
function displayName(name) {
return name || "Anonymous";
}
function count(value) {
return value ?? 0;
}