Free Lesson

What Is a Variable?

A variable is a named container that stores a value so a program can use it and change it later, like a labeled box you can open, read and refill. In math and science the word means something close: a quantity that is unknown or that can change.

Make one yourself below: name a box, put a value in it, and watch a tiny program change it one line at a time.

Interactive lab

A variable is a labeled box

Name the box, choose what goes in it, then press Next to run a three-line program one line at a time. Try a number, a word, or true. Everything runs in your browser.

Language

Valid name. Stored as a number: 10

Program

  1. score = 10
  2. score = score + 5
  3. print(score)

Press Next to run line 1.

Memory

No boxes yet.

Output

 

Spot the variable

Five quick questions across code, math and science.

  1. CodeIn the line age = 12, which part is the variable?
  2. CodeAfter lives = 3 and then lives = lives - 1, what is in lives?
  3. MathIn 2x + 3 = 11, what does the variable x stand for?
  4. ScienceYou give plants 0, 50 or 100 ml of water a day and measure how tall they grow. What is the independent variable?
  5. ScienceIn that same plant experiment, every pot gets the same soil and sunlight. What are those called?

Three meanings

Variables in programming vs math vs science

In programming

A named value that can change while the program runs.

score = 0, then score = score + 10

The name stays the same; the value inside is replaced.

In math

A letter that stands for an unknown or changing number.

2x + 3 = 11, so x = 4

You solve for it, and it does not change partway through the problem.

In science

Any factor in an experiment that can change or be changed.

Independent: water given. Dependent: plant height. Controlled: soil, light.

Change one thing, measure another, keep the rest the same.

Data types

What a variable can hold

TypeAlso calledExample (Python)Use it for
Numberinteger / floatage = 12, price = 4.99Counting, scores, money, measurements
Textstringname = "Ada"Names, messages, anything in quotes
True/falsebooleanis_logged_in = TrueYes/no answers and if checks
Listlist / arrayscores = [90, 72, 85]Many values under one name

A variable that should never change is a constant. JavaScript has const TAX_RATE = 0.08;; Python writes the name in capitals by convention.

Naming rules

How to name a variable

  • DoStart with a letter or underscore: score, _total
  • DoUse names that say what the value is: user_age, not ua
  • DoJoin words: snake_case in Python, camelCase in JavaScript
  • Don'tStart with a digit: 2players
  • Don'tUse spaces or dashes: my score, my-score
  • Don'tUse reserved words: if, for, class, True, let

Names are case-sensitive: Score and score are two different boxes. Type a bad name into the lab above to see the error.

How to

How to create a variable

  1. Step 1

    Pick a clear name

    Choose a name that says what the value is, like score or user_name. Start with a letter and skip spaces.

  2. Step 2

    Choose a starting value

    Decide what goes in the box first: a number like 0, text in quotes like "Ada", or True/False.

  3. Step 3

    Assign it

    Write the name, an equals sign, then the value: score = 0 in Python, or let score = 0; in JavaScript.

  4. Step 4

    Use and update it

    Use the name anywhere you need the value, and assign again to change it: score = score + 1.

Learning to code with AI?

Code Card turns your Claude Code, Codex, and OpenClaw sessions into a public developer profile with activity graphs, token breakdowns, badges, and a shareable card. Start with npx code-card.

Make your Code Card

FAQ

Variable questions, answered

What is a variable in simple terms?

A variable is a named container for a value. In code it is like a labeled box: you put a value in, read it by its name, and swap in a new value whenever you need to.

What is a variable in programming?

In programming, a variable is a name that points to a value stored in the computer's memory. The program uses the name to read the value or replace it, so the same code can work with different data.

What are the 3 types of variables in science?

Independent (the one you change on purpose), dependent (the one you measure) and controlled (the ones you keep the same so the test is fair).

What is the difference between a variable and a constant?

A variable can change while the program runs. A constant is set once and never changes, like const PI = 3.14159 in JavaScript or a name written in CAPITALS by convention in Python.

What are the rules for naming a variable?

Start with a letter or underscore, use only letters, digits and underscores, never use spaces, and avoid reserved words like if, for or class. Names are case-sensitive, so Score and score are two different variables.

Why is it called a variable?

Because its value can vary. The name stays the same while what it holds changes over time, or, in math, stands for a number you have not worked out yet.