The basics of syntax

INPUT · Slides

Changing what is in a variable

01 / 06

Put something else in and it changes

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

The old contents are gone

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

A variable can update itself

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

= does not mean "is equal to"

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

Writing let a second time is an error

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

What is updating actually for

"Put a different value in" sounds dull, but it is how a program comes to have a state.

The score goes up, the time left goes down, the page number moves on. Everything in an app that changes is built out of this.

Once you pair it with the loops coming up, you will see what it is really worth.