BeginnerCoder

Lesson 13 — Ternary & short-circuit

Small decisions on one line

The ternary operator cond ? a : b means "if cond then a, else b". Short-circuit operators skip work: a || b returns a if it is truthy, otherwise b (handy for defaults); a && b returns b only if a is truthy (handy as a guard).

const label = score >= 50 ? "pass" : "fail";
const name = input || "Guest";

"Truthy" means anything except false, 0, "", null, undefined and NaN.

Why it matters: these keep simple choices short and readable.

Want it another way?
Task

Give name a default of "Guest" with ||, and set label to "pass" or "fail" with a ternary (50 or more passes).

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.