01 / 06
Put something else in and it changes
The basics of syntax
INPUT · Slides
01 / 06
A variable made with let can have its contents swapped later.
From the second time on you leave let off and just write name = the new value;.
let score = 10;console.log(score);score = 50;console.log(score);Result
10 50
02 / 06
Put a new value in and the old one does not hang around.
There is only one box, so picture it being written over.
let word = "morning";word = "night";console.log(word);Result
night
03 / 06
Here is the interesting bit. You may write its own name on the right.
count = count + 1 means "take what is in count now, add 1, and put that back into count". It is the standard way to count things.
let count = 0;count = count + 1;count = count + 1;console.log(count);Result
2
04 / 06
Read count = count + 1 as maths and it is nonsense. But it is not an equation, it is an instruction.
= means "work out the right, put it in the left". So count + 1 on the right happens first, and the answer goes into count.
Read it that way and updating suddenly makes sense.
05 / 06
Put let in front when you are updating and you get told off: that name already exists.
let is only for making a new box. To change what is in a box you already have, write just the name.
let score = 10;score = 50;console.log(score);Result
50
06 / 06
let saved = score; puts the number that score holds at that moment into saved.
Updating score later leaves saved at 10. The two variables are not linked to change together. To copy the new number, write saved = score; again.
let score = 10;let saved = score;score = 50;console.log(score);console.log(saved);Result
50 10