Tell Apart Zero, Empty Text, and Missing Data
Scenario
A quiz app receives a student’s score field in three different forms.
Try it yourself
Select each value and see why checking "if (!score)" mistakes a valid zero for missing data.
| Case | Result |
|---|---|
| score = 0 (student answered 0 questions right) | Select a choice above to see what happens. |
| score = "" (blank text box submitted) | Select a choice above to see what happens. |
| score = null / None (field omitted) | Select a choice above to see what happens. |
Reveal the explanation
- score = 0 (student answered 0 questions right)
Valid numeric score! Must return "Try again", NOT "Missing score".
- score = "" (blank text box submitted)
Empty text, not a number. Must return INVALID_SCORE.
- score = null / None (field omitted)
Missing data. Must return INVALID_SCORE explicitly.
Never write "if (!score)" when 0 is a valid input. Check explicitly for null/undefined and integer type so 0 stays a valid score.
Your turn
Explain what happens to freeSeats = 0 if someone writes "if (!freeSeats) return INVALID_FREE_SEATS".
Common mistake Treating 0 free seats as missing input instead of a valid full room.
Check your work
freeSeats = 0 is valid and must return false (Decline), while null/undefined returns INVALID_FREE_SEATS.
Quick check
This browser could not save your progress. Changes last until you leave the page.