Values, Types, and Expressions
Let and Const
Choose between a reassignable let and a fixed const.
Let and Const
let_const.js
let count = 1;
const step = 2;
count = count + step;
const label = "count:" + count;
console.log("step=" + step);
console.log("count=" + count);
console.log("label=" + label);
Follow the Update
countstarts at1.stepis2.count = count + stepchangescountto3.labelbecomescount:3.- The program prints
step=2,count=3, andlabel=count:3. | starting count | step | updated count | label | | --- | --- | --- | --- | | 1 | 2 | 3 | count:3 | | 5 | 2 | 7 | count:7 | | 10 | 2 | 12 | count:12 |
let-const
Use `let` for a binding that will change and `const` for one that stays fixed. Reassigning a `let` updates the value the next expressions see.
Exercise: let_const.js
Reproduce label=count:3, then use the pinned starting counts 5 and 10 to predict each updated label.