Skip to content

Variable

In one line

A variable is a named container for storing data that can change while your program runs.

In simple words

Think of a variable as a labeled box where you store information. You write a name on the box (like age) so you can find it later, and you put a value inside (like 25). Whenever you need that value, you just refer to the box by its name.

The key word is "variable" — the value inside can change. You might start with age = 25 and later update it to age = 26. The name stays the same, but the contents can be replaced at any time during your program.

Variables are the most fundamental building block of programming. Almost every program uses them to keep track of user input, calculation results, counters, and much more. Without variables, your program would have no way to remember anything.

Example

javascript
let age = 25;
console.log(age); // 25

age = 26;
console.log(age); // 26

age is a variable. We first store 25 in it, then update it to 26. The name age stays the same.

How it works

  1. Declare the variable with a name (e.g. let age).
  2. Assign a value to it (e.g. age = 25).
  3. Use the variable by its name — the program reads the current value.
  4. Reassign any time (e.g. age = 26) — the old value is replaced.

Common confusions

  • Confused with: constant

    The difference: A variable can be reassigned to a new value after it is created; a constant cannot change once it is set.

Related terms

Related Resources

Related Lessons

  • Variables

    Learn how variables work in programming

Related Practice

Related Projects

Learn the fundamentals

Deepen your understanding with structured lessons on this topic.

View lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary