A variable is a named container that holds a value your program can use and change later. When you write score = 10, you are telling the computer to store the number 10 under the name score, so you can refer to it, update it, or do math with it elsewhere in your code. Variables are the most fundamental concept in programming: almost everything you write reads from or writes to one. The idea is simple, but the details of types, scope, and mutability are where beginners trip in 2026.
The core idea
Think of a variable as a labeled box. The label is the name, and the contents are the value. You can look inside, swap the contents, or hand the label to another part of the program.
// Declare a variable, then read and update it
score = 10 // store 10 under the name score
score = score + 5 // read it, add 5, store the result back
print(score) // prints 15
The name score does not change; what it holds does. That is the everyday rhythm of code: store something, read it, transform it, store it again.
Variables and types
The value a variable holds has a type. Common ones:
| Type |
Example value |
Use |
| Integer |
42 |
Whole numbers |
| Float |
3.14 |
Decimal numbers |
| String |
"hello" |
Text |
| Boolean |
true |
Yes/no, on/off |
| List / array |
[1, 2, 3] |
Ordered collections |
Some languages are statically typed: you declare the type and the compiler enforces it. Others are dynamically typed: the variable can hold any type, and the language figures it out at runtime. Neither is universally better; static typing catches more mistakes early, dynamic typing is quicker to write. This split shows up directly in TypeScript vs JavaScript in 2026.
Scope: where a variable lives
A variable is only usable within its scope, the region of code where it exists.
- Local scope. A variable declared inside a function exists only there and vanishes when the function returns.
- Block scope. In many languages a variable declared inside an
if or loop block is confined to that block.
- Global scope. A variable declared at the top level is visible everywhere, which is convenient but easy to abuse.
Keeping variables in the narrowest scope that works reduces surprises, because fewer places can change them.
Mutability and constants
A mutable variable can be reassigned. A constant cannot be changed after it is set. Many languages let you mark constants explicitly, signaling that a value is fixed.
Marking things constant when they should not change is a small habit with a big payoff: the reader knows the value is stable, and the language stops accidental reassignment.
How to name variables well
- Describe what it holds.
userEmail beats e. Future you will thank present you.
- Be consistent. Pick a casing style (
camelCase or snake_case) and stick to it.
- Avoid abbreviations that only you understand.
- Use plurals for collections.
users is a list; user is one.
- Do not encode the type in the name in modern code; the type system or context handles that.
Common mistakes
- Reusing one variable for unrelated purposes. It confuses readers and hides bugs.
- Shadowing. Declaring a new variable with the same name as an outer one, then being surprised which one you changed.
- Overusing globals. Anything can change a global from anywhere, which makes bugs hard to trace.
- Off-by-one assumptions when a variable tracks an index or count.
What to skip
- Cryptic single-letter names outside short loops. Clarity costs nothing and pays forever.
- Premature reuse of a variable to save memory; modern hardware does not need it, and it hurts readability.
- Global state for convenience. Pass values explicitly when you can; it keeps code predictable.
FAQ
What is the difference between a variable and a constant?
A variable can be reassigned to a new value; a constant is fixed once set. Marking unchanging values as constants documents intent and prevents accidental changes.
What does scope mean?
The region of code where a variable exists and can be used. Local variables live inside a function or block; global variables are visible everywhere. Narrow scope is safer.
What is a data type?
The kind of value a variable holds, such as an integer, a string, or a list. Some languages enforce types strictly; others infer them at runtime.
Why is naming variables considered hard?
Because a good name compresses meaning. A clear name makes code self-explanatory; a vague one forces every reader to reverse-engineer your intent.
Where to go next
See what a function is in 2026, what an array is in 2026, and what a pointer is in 2026.