L01 Values and assignments
12 minStart here
Outcome
Read literals, names, expressions, and assignments, then state a variable's value after a named line.
Learn
A value is a piece of data such as a number, string, or Boolean. A variable is a name the program uses to refer to a value. The right-hand expression is evaluated before the result is assigned.
In JavaScript, use let for a binding that is reassigned and const for a binding that will not
be reassigned. Python doesn't need either keyword.
Worked example
Predict copy and total after line 4. Start with an empty table, then write each name and its new value.
let total = 4;
total = total + 3;
const copy = total;
total = total - 1;
Predict first, then show the worked explanation
After line 2, total is 7. Line 3 binds copy to the number 7; line 4 changes total to 6.
The earlier copy stays 7.
| After line | total | copy |
|---|---|---|
| 1 | 4 | |
| 2 | 7 | |
| 3 | 7 | 7 |
| 4 | 6 | 7 |
Practice and check
Common mistake
Reading an assignment as a permanent algebra equation. Ask: what is on the right now, and which name receives it?
Next
In the next lesson, follow a value through an if.
References
- JavaScript Guide (MDN)
- The Python Tutorial (Python documentation)