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

  1. count starts at 1.
  2. step is 2.
  3. count = count + step changes count to 3.
  4. label becomes count:3.
  5. The program prints step=2, count=3, and label=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.