JavaScript Variables
A variable is a labeled box that holds a value.
You put something in. Later you can look at it or change it.
Think of a cookie jar with a name tag on the lid.
Make a variable
Put a value in
Change the value
Pick clear names
Declaration
Declaration means making the box and giving it a name.
Here we make a box called savings:
Assignment
Assignment means putting a value inside with =.
The name is on the left. The value is on the right.
Re-assignment
Re-assignment means changing what is inside the same box.
The name stays. The value can grow or shrink.
Declaration and assignment together
You can make the box and fill it in one line.
That is the usual way to start.
Good names
Pick names that tell the truth.
score is clearer than s. userAge is clearer than x.
Your future self will thank you.
Tip: First make the box. Then put something in. Then change it only when you need to.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.