coldwa.st
All guidesProgrammingWebDataToolsDatabasesHaskellConceptsCabal & buildsToolchainCompilerPerformanceEditor & HLS

Programming · fundamentals · concepts

What is a variable?

By ColdwastUpdated Jun 15, 20266 min read#variables#basics#concepts
Code on a computer screen
Code on a screen — variables are the named values that code like this stores, reads and changes.

If you write a single line of code, you'll use a variable. It's the first concept every programming tutorial introduces, and one of the few that appears in every language ever made. This guide explains what a variable is, how you create and use one, types and scope, how variables differ from constants, and why they're the foundation of all programming.

The short definition

A variable is a named container that holds a value your program can read and change. The classic image is a labelled box: the name is the label, and the value is whatever is inside. You put a value in, refer to it by name later, and can replace it with a new value as the program runs.

Declaring and assigning

You declare a variable (create it, give it a name) and assign a value to it. In most languages that looks roughly like this:

age = 30            # name: age, value: 30
name = "Ada"        # a piece of text
age = 31            # reassign — the box now holds 31

After age = 31, the old value is gone and age refers to 31. That ability to change over time is exactly what makes it "variable" — as opposed to a fixed constant.

Types of value

A variable holds a value, and values have types: a number (30), a string of text ("Ada"), a boolean (true / false), or more complex things like lists and objects. Some languages are statically typed (you declare the type, and it's checked before the program runs — like Haskell or Rust); others are dynamically typed (the type travels with the value at run time — like Python or JavaScript).

An open laptop showing code on a desk
An open laptop showing code — every named value you see in a program, from a counter to a username, is a variable.

Scope: where a variable lives

Scope is the region of code where a variable exists and can be used. A variable declared inside a function is usually local — it only exists while that function runs, and code elsewhere can't see it. A variable declared at the top level may be global, visible across the program. Keeping variables in the smallest scope that works is a core habit: it prevents accidental clashes and makes code easier to reason about.

Variables vs constants

A constant is like a variable whose value must not change after it's set — useful for things that shouldn't move, like a tax rate or the number of days in a week. Many languages have a keyword for this (const, final, val). Using a constant when a value won't change signals intent and lets the language catch accidental reassignments.

Why variables matter

Variables let a program remember and work with data instead of dealing only in fixed values. They hold the user's input, a running total, the current state of a game, the result of a calculation. Combine variables with logic and you have an algorithm; combine algorithms with data and you have a program. Everything starts from this small idea: a name that stands for a value.

A couple of honest pitfalls

Two things trip up beginners. First, naming: a good variable name (userAge, not x) makes code readable; vague names are a common source of confusion. Second, mutation surprises: when a variable holds a complex value like a list, two names can point at the same underlying data, so changing one appears to change the other. Understanding when a variable holds a value versus a reference to shared data saves a lot of debugging.

FAQ

What is a variable, simply? A named box that stores a value your program can read and change later by referring to its name.

What's the difference between a variable and a constant? A variable's value can change as the program runs; a constant's value is fixed once set. Use a constant when the value shouldn't change.

What is a variable's type? The kind of value it holds — number, text (string), boolean, list, object, and so on. Some languages check types before running; others track them at run time.

What is variable scope? The part of the code where the variable exists and can be used — typically local (inside a function) or global (across the program). Smaller scope is usually better.

Variables are the data; the step-by-step logic that uses them is an algorithm. Browse more clear explainers in our guides index.

Independent, community-maintained guide. coldwa.st is a programming-resources site; this article is new, original explanatory writing about variables. Syntax varies by language; verify specifics against your language's documentation.