Variables
A named container for a piece of data — you give it a label, put a value inside, and use the label anywhere in your code to refer to that value, read it, or change it.
What is it?
Every program works with data — numbers, text, true/false values, lists of items. Variables are how programs name and store that data so it can be used, modified, and passed around.1
Think of a filing cabinet with labelled drawers. The label “customer_name” tells you what is inside. The contents — “Marie Dupont” — can change. The label stays the same.
Every programming language has variables. The syntax for creating them differs, but the concept is universal:
| Language | Syntax | What it means |
|---|---|---|
| Python | age = 32 | Create a variable named age, put 32 in it |
| JavaScript | let age = 32; | Same thing, different keyword |
| Java | int age = 32; | Same thing, but you must declare the type |
| C | int age = 32; | Same thing, must declare the type |
The syntax is vocabulary. The concept — naming a value — is grammar. Learn the grammar once, and switching languages is learning new vocabulary.2
In plain terms
A variable is a labelled box. You write a name on the outside (the variable name), put something inside (the value), and refer to the name whenever you need what is inside. You can open the box, replace the contents, or tell someone else which box to check.
At a glance
Variable anatomy (click to expand)
graph LR N[Name<br/>age] --> V[Value<br/>32] N --> T[Type<br/>integer] V --> M[Memory<br/>address 0x7ff3] style N fill:#4a9ede,color:#fff style V fill:#5cb85c,color:#fff style T fill:#e8b84b,color:#fffKey: A variable has a name (how you reference it), a value (what it contains), a type (what kind of data), and a memory address (where the computer stores it physically).
How does it work?
Declaration and assignment
Creating a variable has two parts: declaration (telling the program the variable exists) and assignment (giving it a value). Some languages combine these into one step.3
# Python — declaration and assignment in one step
name = "Marie"
# Java — separate declaration, then assignment
String name;
name = "Marie";
After assignment, you use the variable name to access the value:
greeting = "Hello, " + name # greeting is now "Hello, Marie"
Mutability
Most variables are mutable — their value can change:
counter = 0
counter = counter + 1 # counter is now 1
counter = counter + 1 # counter is now 2
Some languages let you create immutable variables (constants) whose value cannot change after assignment:
const PI = 3.14159 # JavaScript — cannot be reassigned
Think of it like...
A mutable variable is a whiteboard — you can erase and rewrite. A constant is a plaque — once engraved, it stays.
Naming conventions
Variable names should describe what they contain. Most languages follow conventions:
| Convention | Example | Used in |
|---|---|---|
snake_case | customer_name | Python, Ruby, Rust |
camelCase | customerName | JavaScript, Java, C# |
UPPER_CASE | MAX_RETRIES | Constants in most languages |
Good names make code readable. x = 32 tells you nothing.
age = 32 tells you everything.
Scope
A variable’s scope defines where in the code it is visible and accessible. A variable created inside a function typically exists only inside that function — it is a local variable. When the function ends, the variable disappears.4
def greet():
message = "Hello" # local to greet()
return message
# message does not exist here — it is out of scope
This boundary prevents variables in one part of the code from accidentally interfering with variables in another part.
Why do we use it?
Key reasons
1. Reuse. Calculate a value once, use it many times. Without variables, you would repeat the calculation everywhere.
2. Readability.
monthly_salary = annual_salary / 12is self-documenting. Without names, code is a wall of numbers.3. Changeability. When a value needs to change (a counter incrementing, a user updating their name), the variable provides a single place to update.
4. Communication. Variables let you pass data between different parts of a program — from one function to another, from input to output.
When do we use it?
- Every time a program stores, reads, or modifies data — variables are the mechanism
- When naming a value makes the code clearer
- When a value will be used more than once
- When a value might change during the program’s execution
Rule of thumb
If you find yourself writing the same number or string more than once, put it in a variable. If you cannot tell what a piece of code does because values are unnamed, add variables.
How can I think about it?
The filing cabinet
Variables are like drawers in a filing cabinet. Each drawer has a label (variable name) and contains a document (value).
You can open a drawer and read the document (
print(age)). You can replace the document with a new one (age = 33). You can tell a colleague which drawer to check (“use thecustomer_namedrawer”).The cabinet has sections (scopes). Drawers in the “kitchen section” (a function) cannot be accessed from the “office section” (another function).
The nametag system
Think of variables as nametags pointing to objects. The nametag
agepoints to the number 32. If you writeage = 33, you peel the nametag off 32 and stick it on 33. The nametag moves; the numbers stay where they are.This is particularly accurate in Python, where variables are references (nametags) pointing to objects in memory, not boxes containing values. Two nametags can point to the same object.
Concepts to explore next
| Concept | What it covers | Status |
|---|---|---|
| data-types | What kinds of values variables can hold | complete |
| functions | How variables are used inside reusable code blocks | complete |
| control-flow | How variables guide decisions and repetition | complete |
| memory-management | Where variables physically live in memory | complete |
Check your understanding
Test yourself (click to expand)
- Explain what a variable is to someone who has never written code. Use the filing cabinet analogy.
- Describe the difference between declaration and assignment. Give an example of each.
- Distinguish between a mutable variable and a constant. When would you use each?
- Interpret this code:
x = 5; y = x; x = 10. What is the value ofyafter these three lines? Why?- Connect variables to memory-management: where does a local variable live (stack or heap) and what happens to it when its function ends?
Where this concept fits
Position in the knowledge graph
graph TD SD[Software Development] --> VAR[Variables] SD --> DT[Data Types] SD --> CF[Control Flow] SD --> FN[Functions] VAR --> DT FN -.->|uses| VAR CF -.->|uses| VAR style VAR fill:#4a9ede,color:#fffRelated concepts:
- data-types — classifies what kind of value a variable can hold
- functions — create local scopes where variables live
- control-flow — uses variables to make decisions and control repetition
Sources
Further reading
Resources
- Teaching Variables: Analogies and Approaches — Khan Academy’s exploration of how to teach variables effectively
- Variables in Python — Real Python’s comprehensive guide to how variables work in Python specifically
- Eloquent JavaScript: Values, Types, and Operators — The opening chapter that introduces variables through JavaScript
Footnotes
-
Sebesta, R. (2019). Concepts of Programming Languages. 12th ed. Pearson. ↩
-
TripleTen. (2024). Basic Coding Concepts: The Complete Beginner’s Guide. TripleTen. ↩
-
Educative. (2024). What Are the Basic Fundamental Concepts of Programming?. Educative. ↩
-
Codecademy. (2024). Overview of Conditionals, Functions, and Scope. Codecademy. ↩
