A false comparison chooses its false branch. Empty text is still a value; only null and undefined need a nullish fallback.
Today’s goal
Explain what each operator checks and which value it returns. Then write a practice check without copying an answer.
0–5 minutes: read the two-choice pattern and explain an equivalent if/else.
5–12 minutes: predict and step through both ternary paths.
12–18 minutes: compare missing values with empty text, zero and false.
18–25 minutes: fix the first two challenges.
25–30 minutes: combine the operators and explain each line aloud. Continue next session if the basics need more time.
Use invented data only. Keep AI assistants closed during practice. Leave live workflows closed; inspect any work code in a separate text file.
Learn first
A ternary chooses one of two values
The ternary operator, also called the conditional operator, has three parts:
the-pattern.jsjavascript · example
condition ? valueWhenTrue : valueWhenFalse
First, evaluate the expression to the left of ?. If it is truthy, evaluate the value after ?. Otherwise, evaluate the value after :. Only the selected branch runs.
Our examples use comparisons such as length >= 5, which return the Boolean values true or false. The condition does not need to be true: false is how we choose the other path.
ternary.jsjavascript · example
let length = 3;let result = length >= 5 ? "Continue" : "Too short";console.log(result);
Read it aloud: “If length is at least five, choose Continue; otherwise choose Too short. Store the chosen value in result.” Here, three is below five, so the log prints Too short.
The same decision written with if/else
if-else.jsjavascript · example
let length = 3;let result;if (length >= 5) { result = "Continue";} else { result = "Too short";}console.log(result);
Both examples assign the same value for the same length. A ternary is an expression that returns a value. An if/else statement chooses a block of statements to execute.
Predict before running: what happens at lengths four, five and six? Pay special attention to >=: exactly five passes.
Watch a choice happen
This is a teaching trace of the supplied examples. The code is read-only here. Use the editable challenges below to write your own code.
Click beside a line number to toggle a red breakpoint. Yellow means “this line runs next.” Step executes that line. Run continues until a breakpoint or the end. Changing the example starts a fresh trace.
Loading the trace…
?? supplies a fallback for a missing value
The nullish coalescing operator checks whether its left value is null or undefined. If so, it evaluates and returns the right value. Otherwise it keeps the left value and skips the right side.
missing-value.jsjavascript · example
let supplied = null;let name = supplied ?? "Guest";console.log(name);
null is an explicit empty or missing value. undefined commonly means a value has not been supplied. An empty string, "", is text with zero characters and is neither of those.
Predict name from supplied ?? "Guest"
supplied
name becomes
Why
null
"Guest"
Nullish
undefined
"Guest"
Nullish
""
""
Keep empty text
0
0
Keep zero
false
false
Keep false
"Milo"
"Milo"
Keep supplied text
Return to the trace and choose Nullish fallback. Try all six inputs. Notice that its TRUE path means “yes, the value is null or undefined,” so that path uses the fallback.
Common mistake:|| also replaces falsy values such as "", 0 and false. It is not interchangeable with ??.
Quick memory check:? and : form a ternary. Two question marks form ??. In a backtick string, ${name} inserts a value; it does not choose a branch.
A different account copy exists. Choose which practice code to keep.
In normal practice, code saves in this browser and, when signed in, to your account. Instructor examples do not save. Each Reset restores only that editor after confirmation. The demonstration trace restarts independently. Checks are practice feedback, not an instructor submission.
Predict · Write · Run · Explain
Make the choices yourself
Change the expressions named in each task. Keep the supplied variable names and values. Run your current code, then Check challenge to test more inputs. Hints are available immediately; explained examples unlock after a pass or three unsuccessful checks.
1. Choose from a condition
Keep length = 3. Replace the result value with a ternary: test length >= 5, choose "Continue" when true and "Too short" when false. Keep the console log.
EDIT JAVASCRIPT
CONSOLE OUTPUT
Predict the result, then choose Run code.
Loading the editor…
Replace this editor’s code with its starting example?
Available after a passing check or three unsuccessful checks. Hints are available now.
2. Supply a missing value
Keep supplied as null. Set name using supplied ?? "Guest". Keep the console log. The fallback must preserve empty text, zero and false.
EDIT JAVASCRIPT
CONSOLE OUTPUT
Predict the result, then choose Run code.
Loading the editor…
Replace this editor’s code with its starting example?
Available after a passing check or three unsuccessful checks. Hints are available now.
3. Build an outbound practice check
Replace the empty text with supplied ?? "". Replace result with a ternary testing text.trim().length >= minimum. Choose "Continue" or "Too short". Keep the supplied data, minimum and log.
EDIT JAVASCRIPT
CONSOLE OUTPUT
Predict the result, then choose Run code.
Loading the editor…
Replace this editor’s code with its starting example?
Available after a passing check or three unsuccessful checks. Hints are available now.
Explain the minimum-length check
Why do we use ?? "" before calling a string method?
What does trim() change, and what stays unchanged?
What does .length measure in these examples?
Which result is chosen exactly at the minimum?
Why does ?? preserve an empty string?
This practice assumes supplied is text, null or undefined. ?? does not convert a number into text. JavaScript string length counts UTF-16 code units; the plain-text examples here avoid emoji and other characters that need a separate discussion.
A response such as "No. I don’t" can pass a length rule. That says nothing about consent, intent or whether to continue a conversation. Interpret its meaning with the surrounding conversation, separately from this programming check.
Coach’s guide and instructor examples
Ask for a prediction before every Step. If a learner needs help, ask “Which question is this operator asking?” before naming the answer. Let the final challenge wait if the two operators are still being confused.
Open instructor examples. This view loads completed practice examples separately from learner drafts and does not save them.
After-session evidence
Explained independently: record the exact idea or example.
Needed help: record the prompt that helped.
Initially incorrect: record the misconception and whether it was corrected.
Final check: independent, prompted, or deferred.
Do not substitute a list of topics covered for evidence of what the learner could explain.