A variable is a named container that holds a value your program can use and change
In programming, a variable is a space in your computer's memory where you store a piece of information — a number, text, true or false, or something more complex. You give that space a name so you can refer to it later. When your program runs, it can read what is stored there, change it, or use it in calculations. Without variables, a program could only work with fixed values written directly into the code, which would make it useless for real work.
Think of a variable like a labeled box. You write a name on the box — say, age or username — and inside you put a value. Later, when you need that value, you just use the name. If you want to change what is inside, you put a new value in the same box. The box stays the same; only what is inside changes.
Key Takeaways
- A variable is a named location in memory that stores a value your program can read, change, or use in calculations.
- Every variable has a name (like score or email), a type (like number or text), and a value (the actual data stored inside).
- You create a variable by declaring it with a name and type, then assign a value to it using the equals sign.
- Variables let you write programs that work with different data each time they run, rather than hardcoding the same values every time.
How you create and use a variable
Creating a variable happens in two steps. First, you declare it by telling the program the variable's name and what type of data it will hold. Second, you assign a value to it. In most languages, this looks like:
age = 25
The name goes on the left, the equals sign means "put the value into this variable," and the value goes on the right. After this line runs, the variable age holds the number 25. Later in your program, you can use age anywhere you would use the number 25. If you write next_age = age + 1, the program reads the value stored in age, adds 1 to it, and stores the result in a new variable called next_age.
Different programming languages have different rules for how you declare variables. Python lets you just write the name and value without stating the type first. Java and C require you to write the type before the name, like int age = 25; (where int means integer). The idea is the same: you are telling the program to set aside space and remember what you put there.
Variable types: what kind of data a variable can hold
Every variable has a type that describes what kind of data it stores. The most common types are:
- Integer: A whole number with no decimal point, like 5, -10, or 1000.
- Float or Double: A number with a decimal point, like 3.14 or 98.6.
- String: Text, like "hello" or "Sarah". Strings are usually written inside quotation marks.
- Boolean: True or false. Used for yes-or-no decisions in your code.
- Array or List: A collection of values, like a list of names or a series of test scores.
The type matters because it tells the program what operations make sense. You can add two numbers together, but you cannot add two text strings the way you add numbers — the program will either refuse or do something unexpected. If you declare a variable as an integer, the program will not let you store text in it (in languages that enforce types strictly). This prevents mistakes.
Why variables matter: making programs flexible
Without variables, every program would have to know all its data in advance. A calculator that only knew how to add 5 and 3 would be useless. With variables, a calculator can ask the user for two numbers, store them in variables, add them, and show the result. The same code works for any two numbers the user enters.
Variables also let you reuse values without typing them over and over. If you need a person's age in ten different places in your program, you store it once in a variable and use the variable name ten times. If the age changes, you only have to update it in one place.
Variables also make code readable. A line that says total_price = base_price + tax is much clearer than x = y + z. The variable names tell you what the code is doing.
Variable scope: where a variable exists and can be used
Scope means the part of your program where a variable exists and can be used. If you create a variable inside a function (a block of code that does one job), that variable usually only exists inside that function. Once the function finishes, the variable disappears. This is called local scope.
A variable created outside any function, at the top level of your program, has global scope — it exists everywhere in the program and any function can use it. Global variables are convenient but can cause confusion if many parts of your program change them, because it becomes hard to track what value they hold at any moment.
Most programmers prefer to keep variables as local as possible — create them where you need them, use them, and let them disappear when you are done. This keeps the program organized and prevents accidents.
Naming variables so your code makes sense
Variable names should describe what the variable holds. user_age is better than ua or x. total_cost is clearer than t. Good names make your code readable six months later when you come back to it and have forgotten what you wrote.
Most languages have rules for variable names: they usually must start with a letter or underscore, can contain letters and numbers, and cannot have spaces. Some languages are case-sensitive, meaning Age and age are two different variables. Check the rules for the language you are using.
Avoid names that are too generic (data, value, temp) unless the variable truly is temporary and only used for a few lines. Avoid names that are misleading — do not call a variable price if it actually holds a quantity.
Changing a variable's value: reassignment
Once you create a variable and give it a value, you can change that value as many times as you want. This is called reassignment. When you write age = 26 after already setting age = 25, the old value (25) is thrown away and replaced with the new value (26).
You can also change a variable based on its current value. age = age + 1 reads the current value of age, adds 1 to it, and stores the result back in age. This is how counters work in loops — a variable starts at 0, and each time the loop runs, you add 1 to it until it reaches the number you want.
Some languages let you declare a variable as constant, meaning its value cannot be changed after you set it the first time. This is useful for values that should never change, like the number of days in a week or the name of your company. Using constants prevents accidental changes and makes your code safer.
Frequently Asked Questions
What is the difference between declaring and assigning a variable?
Declaring means telling the program that a variable exists and what type it is. Assigning means giving it a value. In some languages you do both at once: age = 25. In others you declare first (int age;) and assign later (age = 25;). Both steps are usually needed before you can use the variable.
Can a variable hold different types of data at different times?
In languages like Python, yes — you can put a number in a variable and later put text in the same variable. In languages like Java or C, no — once you declare a variable as an integer, it can only hold integers. Strict typing catches mistakes early but requires more planning. Loose typing is faster to write but easier to get wrong.
What happens to a variable when a program ends?
All variables disappear. The memory they used is freed up for other programs. If you need to save data between program runs, you must write it to a file or database before the program closes.
Why do some variable names use underscores like this_is_a_name?
This style is called snake_case and is common in Python and other languages. Other languages prefer camelCase like thisIsAName. The choice depends on the language's style guide. Pick one style and stick with it so your code looks consistent.
Can two variables have the same name?
Not in the same scope. If you try to create two variables with the same name in the same function, the second one overwrites the first. You can have variables with the same name in different functions or scopes, and they are treated as separate variables, but this is confusing and usually avoided.