What Are Variables? The Basic Coding Concept Every Beginner Needs

The first time someone told me "store that value in a variable," I nodded like I understood. I had absolutely no idea what they meant. A variable is one of the most basic things in coding, and once you get it, everything else starts making sense.

By the end of this page, you'll be able to create, name, and change variables in real code, understand where the value actually lives in your computer's memory, and avoid the five mistakes that trip up every beginner — in plain English, with examples you can actually run.

Infographic explaining variables in coding with examples and steps
Variables

What Is a Variable in Programming?

A variable in programming is a named storage location that holds a value in the computer's memory. You give the variable a name, assign it a value, and use that name to retrieve or change the value anywhere in your program.
How It Works

How Variables Work — Step by Step

1
Declare the variable and give it a name
2
Assign a value — store something inside it
3
Use that value anywhere in your program
4
Reassign — change the value anytime you need to
Real-Life Analogy

Real-Life Analogy — The Labeled Box

Think of variables like labeled boxes
Box 1
name
"Sara"
Box 2
age
25
Box 3
score
100
The label is the variable name. Whatever you put inside is the value. Anytime you need it — just call the label. Simple as that.
Memory

Where Does the Value Actually Go?

The part most tutorials skip — and the reason variables exist at all.

When you create a variable, your program reserves a small slot in the computer's memory (RAM) and puts the value there. Every slot in memory has a raw address — something ugly like 0x7F3A. Without variables, you would have to remember those addresses yourself every single time you wanted to store or fetch a value.

A variable name is simply a human-friendly label for a memory location. You say age — the language quietly translates that to the right address, walks over to that slot, and brings back whatever is inside. That is the entire magic trick.

Your computer's memory (RAM)
0x01
0x02
name
"Sara"
0x03
0x04
age
25
0x05
score
100
Empty cells are free memory. The highlighted cells are slots your variables have claimed. The faded numbers are memory addresses — the language manages them for you, so you only ever deal with names.
Why this matters: once you know a variable is just a labeled slot in memory, everything else — reassignment, scope, even why programs "forget" values when they close — starts making sense.
Want the formal definition too? MDN's Variable glossary entry covers the same idea from a browser-engineering angle.
Declaring Variables

How to Declare a Variable

Create, name, store — here's how it works in JavaScript, Python, and beyond.

Declaring a variable just means creating one. You tell the program — "I need a storage box, here's its name, and here's what goes inside."

Two Words You'll Hear

Declaration vs Initialization — What's the Difference?

Declaration
let age;
Creating the variable — the box exists, but it's still empty.
Initialization
age = 25;
Giving it its first value — the box gets filled.

Most of the time you do both in one line — let age = 25; declares and initializes at once. You'll see both terms in error messages and documentation, so it pays to know them apart.

JavaScript

Declaring Variables in JavaScript

JavaScript gives you three ways to declare a variable — var, let, and const. Use let for values that change and const for values that stay fixed.

variables.js
let name = "Sara"; // can change later
let age = 25; // can change later
const country = "USA"; // never changes
Python

Declaring Variables in Python

Python keeps it simple. No special keyword needed — just write the name, an equals sign, and the value.

variables.py
name = "Sara" # no keyword needed
age = 25 # Python detects the type
country = "USA" # just name = value

How does Python know that 25 is a number and "Sara" is text? This is called dynamic typing — the language works out the type from the value you assign. JavaScript does the same. Languages like Java and C++ are statically typed — you must state the type up front. The kinds of values a variable can hold are called data types in programming, and they get a full lesson of their own.

Don't confuse = with ==. A single = is the assignment operator — it stores a value: age = 25. A double == is the equality operator — it asks a question: age == 25 means "is age equal to 25?" Mixing them up is one of the most common beginner bugs, and we cover both fully in operators in programming.
Across Languages

The Same Variable in 4 Popular Languages

The idea never changes — only the syntax does. Here's how the same variable looks in the four languages beginners meet most often.

LanguageDeclaring a VariableTyping
JavaScriptlet age = 25;Dynamic
Pythonage = 25Dynamic
Javaint age = 25;Static
C++int age = 25;Static
Notice: Java and C++ write int before the name — that's static typing in action. You're telling the compiler the type before the program even runs.
Comparison

var vs let vs const — What's the Difference?

KeywordCan Change?When to UseStatus
var YesOld JavaScript — avoid as a beginnerAvoid
let YesWhen the value needs to changeUse This
const NoWhen the value stays the sameBest Practice
Simple rule: Start with const. If you need to change the value later, switch to let. Avoid var for now.
For the full technical spec on declarations, see MDN's let reference and the official Python tutorial.

What Are Constants in Programming?

Like a variable — but the value is locked forever.

A constant is like a variable — but with one rule: once you set its value, you cannot change it.

Difference

Difference Between a Variable and a Constant

A variable can be updated anytime. A constant is locked the moment you set it. Try to change it and your program throws an error.

Variable — Can Change
let score = 10;
score = 20; // ✓ works fine
Value can be updated
Constant — Locked
const PI = 3.14;
PI = 5; // ✗ error!
Value is fixed forever

One exception to know: in Python, const doesn't exist at all. Constants are just a naming convention — write the name in ALL_CAPS (like MAX_USERS = 10) so other developers know not to change it, but Python won't stop them if they do.

When to Use

When to Use a Constant

Fixed math values — like PI = 3.14
Tax rates or prices that never change
App settings — like max login attempts
Simple rule: If a value should never change — use const. It protects your data from accidental changes.
Naming Rules

Variable Naming Rules Every Beginner Must Know

Must Follow

Rules You Must Follow

Must start with a letter, _, or $
Cannot start with a number — 1name is invalid
No spaces allowed — use camelCase or snake_case
Cannot use reserved words — let, if, return
Case-sensitive — name and Name are different variables
Best Practices

Best Practices for Naming Variables

Good Names
let userName;
let totalPrice;
let isLoggedIn;
let maxRetries;
Bad Names
let x;
let a1;
let temp;
let data;
JavaScript
totalPrice
camelCase — first word lowercase, next words capitalized
Python
total_price
snake_case — all lowercase, words separated by underscore
Classes
TotalPrice
PascalCase — every word capitalized; reserved for class names, not variables
Golden rule: Name your variable so clearly that anyone — including future you — knows exactly what it holds without reading any other code.
Scope

Variable Scope — Local vs Global

Where you create a variable decides where you can use it.

Scope means — where in your program a variable can be used. Not every variable is available everywhere. Where you create it decides where you can use it.

Seeing "function" for the first time? Don't worry — functions in programming get their own full lesson later in this series. For now, just think of a function as a reusable block of code wrapped in { } braces.
Global

Global Variables

A global variable is created outside any function. It can be used anywhere in your program — from top to bottom, inside or outside functions.

Local

Local Variables

A local variable is created inside a function. It only exists inside that function. Try to use it outside — your program throws an error immediately.

Global Scope
let globalName = "Sara"; // accessible everywhere
Local Scope — inside function
function greet() {
let localMsg = "Hello!"; // local only
console.log(globalName); // ✓ works
}
console.log(localMsg); // ✗ error — not accessible here
✓ globalName — everywhere
✓ localMsg — inside function only
✗ localMsg — outside function = error
Simple rule: Global variables work everywhere. Local variables only work inside the block of code where they were created.
One level deeper (for the curious): in modern JavaScript, let and const are technically block-scoped — they live inside the nearest pair of { } braces, which includes functions, loops, and if-statements. And if you create a local variable with the same name as a global one, the local version "wins" inside its block — that's called variable shadowing, and it's legal but confusing to read, so most style guides avoid it.
Troubleshooting

5 Variable Mistakes Every Beginner Makes

And the exact error messages they cause — so you recognise them instantly.

Every programmer has made all five of these. Learning to read the error message is half the skill — here's what each one looks like and how to fix it.

1

Using a variable before creating it

console.log(userName); // used first...
let userName = "Sara"; // ...declared after
ReferenceError: Cannot access 'userName' before initialization
Fix: Always declare a variable above the first line that uses it. Code runs top to bottom.
2

Trying to change a constant

const maxUsers = 10;
maxUsers = 20; // nope!
TypeError: Assignment to constant variable.
Fix: If the value genuinely needs to change, declare it with let instead of const.
3

The capital-letter typo

let userName = "Sara";
console.log(username); // lowercase n — different variable!
ReferenceError: username is not defined
Fix: Variable names are case-sensitive. userName and username are two completely different variables — check every capital letter.
The first bug that ever ate an hour of my life was exactly this — one capital letter. I checked everything except the spelling. Now it's the first thing I check.
4

Using = when you meant ==

if (score = 100) { // assigns 100 instead of comparing!
  console.log("Perfect score!");
}
No error at all — the worst kind of bug. It silently overwrites your value.
Fix: Use == (or better, === in JavaScript) when comparing. Single = only ever stores a value.
5

Declaring the same variable twice

let score = 10;
let score = 20; // re-declared with let!
SyntaxError: Identifier 'score' has already been declared
Fix: Declare once, reassign after. The second line should simply be score = 20; — no let.

FAQs About Variables in Programming

Quick answers to the questions beginners ask most

Q 01

What are the variables in a code?

Variables in a code are named storage spaces that hold values you can use later. Each variable has a name and a value — the name acts like a label, and the value is whatever you store inside. You can use that value anywhere in your program, change it whenever needed, and rely on the name to fetch it back.
example.js
let userName = "Sara"; // name + value
let userAge = 25; // another variable
console.log(userName); // prints "Sara"
In short: A variable = a label + a value. The label helps you find it, the value is what you stored.
Q 02

What are the 4 types of variables in programming?

This question actually has two answers, because "type" means two different things. If you mean types of variables, programmers classify them by where they live and how long they exist — the four you'll meet are local, global, instance, and static variables (the first two are covered in the scope section above; instance and static appear when you reach object-oriented programming).
If you mean the types of values a variable can hold, those are called data types — and the four every beginner meets first are:
String
"Sara", "Hello"
Number
25, 3.14, 100
Boolean
true, false
Array / Object
["red", "blue"]
The variable is the box; the data type describes what kind of thing is inside it. We break down every data type with examples in our full guide to data types in programming.
Q 03

Can a variable store multiple values?

Yes — but not by itself. A regular variable holds one value at a time. To store multiple values, you use special types called arrays or objects.
multiple-values.js
// Array — stores a list of values
let colors = ["red", "blue", "green"];

// Object — stores key-value pairs
let user = { name: "Sara", age: 25 };
Both get their own full lessons — see arrays in programming and objects in programming. For now, just know that they can hold many values inside a single variable.
Q 04

What is the difference between a variable and an identifier?

They're closely related but not identical. An identifier is the formal term for any name you create in code — a variable name, a function name, a class name. A variable is specifically an identifier that's attached to a storage location holding a value. So every variable name is an identifier, but not every identifier is a variable — greet in function greet() {} is an identifier for a function, not a variable.
Why it matters: you'll see "identifier" in official error messages (like SyntaxError: Identifier 'score' has already been declared above) — now you know exactly what it's referring to.
Still have questions? Browse our free tutorials for clear answers.
Browse Tutorials →
You Made It

What You Learned About Variables

A variable is a named storage location in memory — a label for a slot in RAM.
You declare it, initialize it with a first value, then use or reassign it anywhere in scope.
Use const by default, let when the value must change — and forget var for now.
Names are case-sensitive and should describe exactly what they hold.
Scope decides where a variable can be used — global works everywhere, local stays in its block.
Teaching a child to code? This exact lesson also exists in a playful, visual version — visit our For Kids section and meet Boxy, the variable who loves storing things.