← Back to Programming Fundamentals
04

Variables and State: Storing Information So a Program Can Think

Learn what a variable is, how it represents state inside a program, and how to use names and value changes clearly so you do not get lost at the beginning.

📘 Theory

What a Variable Really Is

A variable is not magic. It is a way of naming a piece of data so you can use it later.

If a program needs to remember a name, a score, a total, or the number of login attempts, it needs some logical place to store that information. That is the job of a variable.

A useful beginner model is to think of a variable as a label attached to the current value the program cares about.

  • The variable has a name.
  • The value is its current content.
  • That content can stay the same or change.
  • The name should help explain what it represents.

State Is What the Program Knows at a Specific Moment

Talking about state means talking about the current data the program is working with.

1

If a task app knows how many tasks exist, that number is part of its state. If a counter shows 3 clicks, that 3 is also state.

2

This helps you stop seeing variables as isolated pieces and start seeing them as the minimum memory a program needs in order to keep working.

Declare, Read, and Update a Variable

At this stage you do not need many rules. You need to see clearly when a variable appears, what it stores, and how it changes.

1

The important part here is not only the syntax of `let`, but the idea that the program begins with one value and then continues with another.

2

That change is your first real contact with mutable program state.

A Good Variable Name Is Already a Technical Decision

A bad name forces you to guess. A good name saves you reading and debugging time.

  • `cartTotal` is better than `x`.
  • `activeUser` is better than `data1`.
  • `loginAttempts` is better than `counter` when context matters.
  • Avoid vague names that could mean almost anything.

🧭 Key visuals

A variable as a box of state

It helps explain that a variable stores a current piece of program state and that this state may later be updated.

A teaching diagram that represents a variable as a labeled box whose value can change over time inside a program.

🧪 Learn by doing

Example Guided Example: A Tiny Counter Watch how one variable represents the current state of a very simple counter.

🏁 Challenges

Challenge Challenge: Model the State of a Scoreboard Create a variable that represents a player's score and update it once.

🧰 Resources

Test

Check your knowledge with a test about Programming Fundamentals.

Test for Programming Fundamentals

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com