AI Hints
JavaScript · Lesson 5 of 138

Variables with let

Boxes for values.

⏱ 2 min read▶ Video at 4:27🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

A variable is a named box that holds a value. You put a value in, and later use the name to get it back. You can even replace what's in the box. In modern JavaScript, we make changeable variables with let.

Variables with let
From the video: Variables with let
  • A named box
  • Put a value in, get it back
  • let

Making and changing a variable

Let score equals zero creates a box called score, holding zero. Later, we put a new value in it, without writing let again. And score equals score plus ten takes the old value, adds ten, and stores the result. Run it.

app.jsjavascript
let score = 0;
console.log("Start:", score);

score = 50;
console.log("Now:", score);

score = score + 10;
console.log("After bonus:", score);
Output
Start: 0
Now: 50
After bonus: 60

Shortcuts

Adding to a variable is so common that there's a shortcut. Score plus equals ten means score equals score plus ten. Minus equals, times equals, and divide equals work the same way. And plus plus adds exactly one, which is handy for counting.

keyvalue
x += 10x = x + 10
x -= 3x = x − 3
x *= 2x = x × 2
x++add one

Summary

Let creates a variable whose value can change. Use equals to give it a new value. Next, const, for values that shouldn't change.

Key takeaways
  • let name = value
  • name = new value
  • += and ++
Next up: const — values that don't change