A total does not update itself
A program calculates a total, then changes quantity. Will the previously calculated number automatically change? Predict first.
let quantity = 2;
const unitPrice = 20;
const firstTotal = unitPrice * quantity;
quantity = 3;
const revisedTotal = unitPrice * quantity;
console.assert(firstTotal === 40);
console.assert(revisedTotal === 60);
An expression is evaluated when that statement runs. firstTotal stores the resulting number, not a live spreadsheet formula. Changing quantity does not repeat earlier instructions.
Trace with a table
Write quantity and any available totals after each statement. Before a declaration executes, do not assume its initialized value exists. The useful mental model is the current state plus the next instruction, not all source lines happening simultaneously.
Function declarations describe callable behavior; their bodies run when called. Passing a callback as a value does not execute its body until the receiving operation calls it.
Guided review
To produce a new quote, call the calculation with the new inputs or evaluate the steps again. Do not expect const to make a value reactive. A reactive interface is a separate system we are not building here.
Independent exercise
A subtotal is 50. Calculate a 10% discount, then set the subtotal to 100 and display the old discount. Predict what appears and describe the correct workflow for a revised quote.
Correction
The old discount remains 5. Recalculate after changing the inputs to obtain 10. Alternatively, keep each quote's inputs and derived values together so old and revised quotes cannot be mixed accidentally.
Review
Execution-order reasoning is the basis for debugging and asynchronous code. Write down which values exist at each step before considering operations that complete later.