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. This guide explains exactly what a variable is, how to create one, and how to use it — in plain English, with real examples.

📦 Variables

What Is a Variable in Coding?

📦
A variable is a named storage location in your program that holds a value. You give it a name, store something inside it, and use it whenever you need that value.
⚙️ How It Works

How Variables Work — Step by Step

1
✏️
Create the variable and give it a name
2
📥
Store a value inside it
3
🔍
Use that value anywhere in your program
4
🔄
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.
Infographic explaining variables in coding with examples and steps
⌨️ Declaring Variables

How to Declare a Variable

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

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.”

⚡ 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. Python figures out the rest automatically.

🐍 variables.py
name = “Sara” # no keyword needed
age = 25 # Python figures it out
country = “USA” # just name = value
⚖️ 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.
🔒

What Are Constants in Coding?

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
✅ 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
💡 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.

🌍 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 function where they were created.

FAQs About Variables in Coding

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?

+
The four main types beginners need to know are String, Number, Boolean, and Array / Object. These four cover almost every real-world need when you start learning to code.
📝 String
“Sara”, “Hello”
🔢 Number
25, 3.14, 100
✅ Boolean
true, false
📦 Array / Object
[“red”, “blue”]
Each type stores a different kind of data. Choosing the right type is the first step toward bug-free code.
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 };
💡 Arrays and objects are covered in detail on their own pages — for now, just know that they can hold many values inside a single variable.
Still have questions? Browse our free tutorials for clear answers.
Browse Tutorials →