Coding for All
Lesson 3: Changing Values with let
Changing Values with let Use let for values that change.
1let score = 0;
2score = score + 10;
3console.log(score);
Terminal
$ node main.js
10
Every line, explained
let score = 0;
let creates a variable whose value you PLAN to change later. The modern rule of thumb: use const unless you know the value will change; then use let. (You may see var in old code; it went out of style years ago.) Our score starts at 0 but will go up, so it gets let.
score = score + 10;
No let or const here: score already exists, we are just giving it a new value: 0 + 10 = 10. Trying to do this to a const would be an error!
console.log(score);
console.log(...) prints whatever is inside the round brackets to the terminal, then moves to a new line. console is the terminal itself, log is the tool that writes to it, joined with a dot.