Beginner Topic 1 of 54 Programming Fundamentals
Variables
Named storage locations holding values that can change during execution.
How it works
A variable binds a name to a location in memory; assigning to it updates the value stored there. In statically typed languages the type is fixed at declaration, while in dynamic languages the same name can refer to different types over time. Under the hood, the compiler or interpreter maps each name to a slot on the stack, a register or the heap, and reading the name fetches whatever value currently lives there. Scope and lifetime determine where the name is visible and how long the value survives.
Step-by-step walkthrough
- The name x is declared and bound to a slot in memory.
- The value 5 is stored in that slot (x = 5).
- Reading x fetches whatever the slot currently holds.
- Assigning x = 9 overwrites the slot, the old value is gone.
- Every later read of x now sees 9.
Where it is used
Counters, accumulators, loop indexes, user input, configuration values and intermediate results are all held in variables, for example total = total + price inside a shopping-cart loop. Well-chosen variable names double as documentation, letting readers follow the logic without extra comments.
Common pitfall
Using a variable before assigning it, or accidentally shadowing an outer variable with an inner one of the same name, causes subtle bugs. Reusing one variable for unrelated purposes makes code confusing. Choose descriptive names, declare variables close to first use and prefer constants when a value never changes.
Where it fits in the course
Variables is topic 1 of 54 in Programming Fundamentals, rated Beginner. It is the first topic in this area. The next topic is Data Types.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Programming Fundamentals quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Variables?
Where do people use Variables?
What is the most common mistake with Variables?
What should I learn before this topic?
How can I practice it?
Is Variables hard to learn?
How long does it take to learn Variables?
Which topics are related to Variables?
What should I study after Variables?
Related topics
- Data Types (Programming Fundamentals)
- Operators (Programming Fundamentals)
- Expressions (Programming Fundamentals)
- Type Casting (Programming Fundamentals)
- Environment Variables (DevOps & Tooling)
- Condition Variables (Concurrency & Parallelism)
- Random Variables (Math for Computing)