Operators & short-circuiting
Understand the idea
A saved volume of zero is replaced by the default volume. The fallback treated a valid value as missing.
Read an expression as a decision
An operand is simply a value an operator works on. In price + fee, the names provide the operands and + is the operator. An expression produces a value; a describes a complete step in the program. This lets you put a calculation inside a larger decision without storing every tiny result separately.
Arithmetic has an order
Arithmetic operators produce numbers, except that + can also concatenate strings. Assignment with = stores a value; comparison with === checks a relationship. Confusing the two can turn a condition into a state change. A comparison returns a boolean, which control flow can use.
Multiplication happens before addition unless parentheses change the grouping. Do not rely on a reader remembering every precedence rule. Parentheses around the part you mean to evaluate together make a dense calculation easier to check.
| Family | Operators | Result or behavior |
|---|---|---|
| Arithmetic | + - * / % ** | Calculate a value; + can also join strings |
| Assignment | = += -= | Store or update a binding/property |
| Comparison | === !== < <= > >= | Produce a boolean |
| Logical | &&, logical OR, ! | Short-circuit or negate truthiness |
| Nullish/default | ?? | Use the right operand only for null or undefined |
| Optional access | ?. | Stop an access when the left value is nullish |
| Conditional | condition ? yes : no | Choose one of two expressions |
Comparisons versus assignments
Precedence determines how an expression is grouped: multiplication groups before addition. Parentheses make your intended grouping explicit. Do not rely on someone remembering a long precedence table when a pair of parentheses makes the decision tree obvious.
Short-circuit decisions
Logical && and || . They may skip the right side, and they return an operand, not necessarily a boolean. || uses the right side when the left side is , including zero, false, and an empty string. ?? uses it only for or . Choose the fallback rule your data actually needs.
Missing values and defaults
Optional chaining, ?., stops a access or call when its left side is . It does not declare a missing or validate a value's entire shape. A ternary expression chooses one of two values; reserve it for decisions short enough to read at a glance.
Read a fallback aloud: “use this setting unless it is missing.” That usually means ??. If you really mean “use this setting unless it is falsy,” use ||. The difference matters for a volume of zero, an unchecked checkbox, and an intentionally empty label.
Mental model
FOLLOW THE VALUE
Zero remains a valid value.
01 / 03Trace the transformation
Live example
Replacing ?? with || changes only the first log, but that difference is enough to overwrite a user’s intentional zero setting.
Your console output appears here.
Guided challenge
Preserve a valid zero
Choose a fallback operator that supplies 50 only when savedVolume is null or undefined.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
0 18 true
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.