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:

LanguageSyntaxWhat it means
Pythonage = 32Create a variable named age, put 32 in it
JavaScriptlet age = 32;Same thing, different keyword
Javaint age = 32;Same thing, but you must declare the type
Cint 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


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:

ConventionExampleUsed in
snake_casecustomer_namePython, Ruby, Rust
camelCasecustomerNameJavaScript, Java, C#
UPPER_CASEMAX_RETRIESConstants 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 / 12 is 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 the customer_name drawer”).

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 age points to the number 32. If you write age = 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

ConceptWhat it coversStatus
data-typesWhat kinds of values variables can holdcomplete
functionsHow variables are used inside reusable code blockscomplete
control-flowHow variables guide decisions and repetitioncomplete
memory-managementWhere variables physically live in memorycomplete

Check your understanding


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:#fff

Related 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

Footnotes

  1. Sebesta, R. (2019). Concepts of Programming Languages. 12th ed. Pearson.

  2. TripleTen. (2024). Basic Coding Concepts: The Complete Beginner’s Guide. TripleTen.

  3. Educative. (2024). What Are the Basic Fundamental Concepts of Programming?. Educative.

  4. Codecademy. (2024). Overview of Conditionals, Functions, and Scope. Codecademy.