LESSON 3

Variables

Learning objective: Store and reuse data with variables.

Understand

Variables are named containers for values.

Declare variables with let for values that can change. A variable has a name and holds a value you can read and update later. Use clear, descriptive names.

Analogy: A variable is like a labelled jar you can put something into and take it out later.

See It in Action

Declaring and updating:

let score = 0;
score = score + 10;
console.log(score); // 10
How it works: score starts at 0, then is reassigned to 10; console.log prints its current value.

Try It Yourself

  1. Create a let variable for your name.
  2. Log it to the console.
  3. Reassign it and log again.

Quick Quiz

Which keyword declares a variable that can change?

Challenge

Track a running total in a variable and log the result.

Success condition: Your variable stores a value and logs an updated result.