Variables and Values: Store and Update Data
Remember a mark, then correct it
A teacher records a science mark of 35 out of 50. After reviewing the paper, she adds five marks. The program must remember the original mark and the corrected mark rather than asking someone to calculate them again.
A variable gives a value a name so instructions can use it later. Assignment stores or associates a value with that name. In languages that allow reassignment, a later instruction can associate the same name with a new value.
Read an assignment
Identify the name, current value and calculation in a mark update.
Trace a correction
Explain why the corrected mark changes while an earlier numeric copy remains the same.
Choose a declaration
Use JavaScript let for reassigned values and const for names that should not be reassigned.
Follow the changing science mark
Before the correction
The name science lets us refer to the stored mark.
science → 35Read, calculate, assign
Use the old mark to calculate the corrected value.
35 + 5 → 40After the correction
A later read of science gets the new mark.
science → 40
A name is different from the value it refers to
In let science = 35;, science is the name and 35 is the value. let declares the name in JavaScript, and the initial assignment supplies its starting value. The semicolon ends this statement.
Later instructions can read science instead of repeating 35. This helps us express the school task: science tells a reader what the number means, while an unrelated name such as x does not.
You can imagine a labeled place for a value, but do not take that picture literally. Languages can associate names with values in different ways, and objects can be shared through several names. For now we are tracing ordinary numeric marks. Revisit the student marks calculation if stored values are new.
Trace the original and corrected science marks
This complete program uses JavaScript. let permits another assignment to science. const declares a name we will not reassign. console.log displays the current value supplied to it.
Predict both output lines before revealing the trace. The second sample uses a different starting mark but the same correction rule. The player uses prepared states for these programs; it is not a live editor or debugger.
Choose a sample and follow its execution. Guided simulation.
let science = 35;
const originalScience = science;
science = science + 5;
console.log(science);
console.log(originalScience);Top-level scope
Display the original mark, 35. The two names now hold different numeric values.
40 35
What do you expect this program to print?
Assignment is an action, not an equation
JavaScript · read the old mark before assigning the new one
let science = 35;
const originalScience = science;
science = science + 5;
console.log(science);
console.log(originalScience);Expected output
40
35The third line, science = science + 5;, may look impossible if you read = as mathematical equality. In this JavaScript statement it means assignment: first evaluate the right side, then store the result using the name on the left.
With a current mark of 35, read 35, calculate 35 + 5 = 40, then associate science with 40. Reading the old value happens before replacing it. This instruction does not add five forever; it adds five each time execution reaches that statement.
Comparing values uses different operators, introduced later in comparisons. JavaScript’s shorter science += 5 form is covered in assignment operators. Learn the expanded form first so you can trace what changes.
An earlier numeric copy is not a live formula
JavaScript · copy the original numeric mark
let science = 35;
const originalScience = science;
science = science + 5;
console.log(science);
console.log(originalScience);Expected output
40
35The second line reads the current numeric value of science and stores it as originalScience. At that moment both values are 35. A later assignment to science does not go back and repeat the second line.
The final output is therefore 40 followed by 35. The original mark remains available for a report explaining the correction. If you needed a value recalculated on demand, you would write another calculation or use a function; ordinary assignment does not create a spreadsheet-style formula.
This explanation is about numeric values. Assigning an object to another name can make both names refer to the same object. We will compare that behavior in collections, records and shared objects.
Use let for reassignment and const for a fixed association
This JavaScript version stores the maximum mark as const because that name is not reassigned. The science mark uses let because a later instruction corrects it.
const does not mean “a value the computer knows before execution.” Its starting value can come from a calculation. It means that this name cannot be assigned another value after its declaration. A JavaScript const declaration needs an initial value.
The example prints 40 and 50. If you tried to reassign maximumMark, JavaScript would report an error. The const documentation explains why this restriction does not freeze the contents of an object.
JavaScript · local teaching example
const maximumMark = 50;
let science = 35;
science = 40;
console.log(science);
console.log(maximumMark);Expected output
40
50Apply a second correction
JavaScript · starting program for the correction task
let science = 35;
const originalScience = science;
science = science + 5;
console.log(science);
console.log(originalScience);Start with the original program shown above. After adding five marks, subtract two marks before either value is displayed. What should be printed? Work through the assignments before reading the answer.
Worked answer
The corrected mark first becomes 40 and then becomes 38. The original copy remains 35. Add the second assignment after the first correction; changing the original copy would lose the record of the starting mark.
The complete worked program below prints 38 and 35.
JavaScript · local teaching example
let science = 35;
const originalScience = science;
science = science + 5;
science = science - 2;
console.log(science);
console.log(originalScience);Expected output
38
35Names and initialization can cause mistakes
Choose names that describe the information. scienceMark is clearer than s. In JavaScript, names cannot contain spaces or start with a digit, and language keywords cannot be ordinary variable names. Names are case-sensitive: science and Science are different.
Declare a name before using it. A JavaScript let or const read before its declaration is reached produces an error. let science;, after its declaration is reached, instead has the value undefined until assigned. That missing value is not a mark of zero.
A variable is not automatically available everywhere. The region where a name can be used is its scope. JavaScript’s let and const are scoped to their enclosing block. Follow variable scope for complete examples, and see the let documentation for the language-specific rules.
Check: what happens to the original mark?
JavaScript · values for this question
let science = 35;
const originalScience = science;
science = science + 5;
console.log(science);
console.log(originalScience);Check: choose a declaration deliberately
Read the current value, calculate, then assign
A variable gives a value a useful name. Assignment evaluates a value and associates it with that name; reassignment can change what later reads retrieve. A numeric copy made earlier does not update itself.
In JavaScript, choose let for names you will reassign and const for names you will not. Neither declaration replaces checks on whether a mark is valid.
Explain why adding five and subtracting two produces 38 while the original mark stays 35. Next, learn which kinds of values a name can hold. For a different language’s syntax, continue with the Python course.
Reaching the recap and completing the quizzes are separate milestones.