What Are Variables in Programming? A Simple Explanation
Learn what variables are in programming with clear examples. Understand how to store, update, and use data in your code.
Learn2Code Team
December 14, 2025
Variables Explained Simply
A variable is a container for storing data. Think of it like a labeled box where you can put something, take it out, or replace it with something else.
1let score = 0; // A box labeled "score" containing 02score = 10; // Replace the contents with 103score = score + 5; // Now it's 15Why Do We Need Variables?
Without variables, programs couldn't remember anything. Imagine a calculator that forgot numbers the moment you entered them—useless.
Variables let your program:
- Remember user input - Store a name someone typed
- Track state - Keep count of items in a cart
- Perform calculations - Store intermediate results
- Make decisions - Check if a user is logged in
Declaring Variables
Different languages have different syntax, but the concept is the same:
1// JavaScript2let name = "Sarah";3const age = 28;4var oldStyle = "avoid this";1# Python2name = "Sarah"3age = 28let vs const vs var
In JavaScript:
- let - Value can change later
- const - Value cannot change (constant)
- var - Old way, avoid in modern code
1let score = 0;2score = 100; // Works fine3 4const pi = 3.14159;5pi = 3; // Error! Can't change a constNaming Variables
Good variable names make code readable:
1// Bad - unclear what these mean2let x = "John";3let n = 25;4let t = true;5 6// Good - self-documenting7let userName = "John";8let userAge = 25;9let isLoggedIn = true;Naming Rules
- Start with a letter, underscore, or dollar sign
- Can contain letters, numbers, underscores
- Case-sensitive (
nameandNameare different) - Can't use reserved words (
let,function,if)
Naming Conventions
- camelCase - JavaScript standard:
firstName,totalPrice - snake_case - Python standard:
first_name,total_price
Data Types
Variables can hold different types of data:
1// String - text2let greeting = "Hello, world!";3 4// Number - integers and decimals5let count = 42;6let price = 19.99;7 8// Boolean - true or false9let isActive = true;10 11// Array - list of items12let colors = ["red", "green", "blue"];13 14// Object - structured data15let user = {16 name: "Alex",17 age: 3018};Common Mistakes
Mistake 1: Using before declaring
1console.log(name); // Error!2let name = "Sam";Mistake 2: Forgetting quotes for strings
1let city = New York; // Error!2let city = "New York"; // CorrectMistake 3: Using = instead of == for comparison
1if (score = 100) { // This assigns, doesn't compare!2if (score === 100) { // This compares correctlyPractice Makes Perfect
Understanding variables is the foundation of programming. Every program you write will use them.
The best way to learn is to practice. Try creating variables for different scenarios:
- Store your name and age
- Create a shopping list array
- Track a game score that increases
Start with our interactive exercises to build your variable skills through hands-on practice.
