How come a variable's value is fixed to the first declaration when used within another variable?
When you use a variable in an expression, the result of that expression is assigned to another variable. Expressions are evaluated based on the current values of the variables they contain. This behavior is fundamental in JavaScript and other imperative/procedural languages.
Expressions do not dynamically update when their inputs change, nor do variables automatically update themselves based on past assignments. If you want a calculation to be redone when its inputs change, create a function and call it as needed. Functions are dynamic definitions of calculations that can be updated by changing their inputs.
Is there a way to reference a variable within another variable without capturing the value at assignment?
A variable simply holds a value and does not retain information about how or when it was assigned. It cannot store a reference to another variable. Variables are like boxes that hold values, without any built-in mechanism for self-updating or referencing other variables directly.
What is the best approach to handle this situation?
This behavior is intrinsic to JavaScript and not something to avoid. Understand how variables and expressions work to effectively manage your code.