Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 06 / 34

Operators & short-circuiting

01

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.

Operator families and their results
FamilyOperatorsResult 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
Conditionalcondition ? yes : noChoose 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.

02

Mental model

FOLLOW THE VALUE

savedVolume = 0
nullish?
no

Zero remains a valid value.

01 / 03Trace the transformation

03

Live example

Replacing ?? with || changes only the first log, but that difference is enough to overwrite a user’s intentional zero setting.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Preserve a valid zero

Choose a fallback operator that supplies 50 only when savedVolume is null or undefined.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

0
18
true

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Which value causes ?? to use its fallback?
    02What does false && expensiveWork() do?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson