Learning object 8 of 100
Logical operator explorer
Choose two values and see what &&, ||, ?? and ! return, whether each value is truthy, and whether the right side was evaluated at all.
Loading…
What it shows
&& and || return one of their operands, not always a boolean. a || b gives a when a is truthy, otherwise b. a && b gives a when a is falsy, otherwise b. a ?? b falls back to b only when a is null or undefined. When the left side decides the answer, the right side never runs: short-circuit evaluation.
Guided experiment
- Try
""and"Guest". Why is||a common way to give a default name? - Try
0and10. Compare||with??. Which keeps 0? - Find a pair where all three operators skip the right side.
Predict
What is 0 ?? 5?
Related
Lessons: Lesson 12: Logical operators
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-008.js in the companion repository.