A variable is a named container that holds a value your program can use and change

In programming, a variable is a labeled box in your computer's memory that stores a piece of information. When you write code, you give that box a name—like age or temperature—and put a value inside it. Your program can then read that value, change it, or use it in calculations. Without variables, you would have to write out the same number or word over and over, and you would have no way to store information that changes while your program runs.

Think of a variable like a whiteboard with a label on it. You write a number on the board, erase it, and write a new number. The label stays the same, but what is written on it changes. That is exactly how a variable works in code.

Key Takeaways

  • A variable has a name you choose and a value that can be a number, text, true or false, or other data types.
  • You create a variable by declaring it with a keyword like var, let, or int, depending on the programming language.
  • You can change a variable's value at any point in your program, and the new value replaces the old one.
  • Variable names should be descriptive so that anyone reading your code understands what information the variable holds.

How to create and use a variable

Creating a variable involves two steps: declaring it and assigning a value to it. In most languages, you write a keyword (like let in JavaScript or int in Java), then the name you want to give it, then the value. For example, in JavaScript you might write let age = 25; This tells the computer to create a space in memory called age and put the number 25 in it.

Once you have created a variable, you can use its name anywhere in your code to refer to the value inside. If you write console.log(age); the program will print 25 to the screen. If you later write age = 30; the value changes to 30, and from that point forward, age refers to 30, not 25.

Different programming languages have slightly different syntax—the exact words and symbols you use—but the idea is the same. You name a container, put something in it, and refer to it by name whenever you need that value.

Data types: what kind of information a variable can hold

Variables can hold different kinds of information, called data types. The most common are numbers (called integers or floats), text (called strings), and true-or-false values (called booleans). Some languages require you to tell the computer which type a variable will hold when you create it; others figure it out automatically based on what you put in.

A number variable might hold 25 or 3.14. A string variable holds text, written inside quotation marks: "hello" or "John Smith". A boolean variable holds only true or false, useful for yes-or-no questions like isRaining = true. As you write more code, you will encounter other data types like lists and objects, but these three are the foundation.

Variable names: choosing a name that makes sense

You can name a variable almost anything you want, but good names make your code easier to read and fix later. A name like x tells you nothing; a name like userAge or totalPrice tells you exactly what the variable holds. Most programmers use lowercase letters and capitalize the first letter of each new word after the first one—a style called camelCase.

Avoid names that are too short or too vague. a, temp, or data force anyone reading your code to hunt through the surrounding lines to understand what the variable does. customerName, shippingCost, and isLoggedIn are clear at a glance. When you come back to your own code weeks later, you will be grateful for names that explain themselves.

Changing a variable's value during the program

One of the most powerful things about variables is that you can change their value as your program runs. This is called reassignment. If you start with count = 0 and then write count = count + 1, the variable count now holds 1. You can do this as many times as you need.

This is how programs keep track of things that change: a score in a game, the number of items in a shopping cart, the current temperature reading from a sensor. Without the ability to reassign variables, programs could only work with fixed numbers and could not respond to what happens while they run.

Scope: where a variable can be used

Scope is the part of your program where a variable exists and can be used. If you create a variable inside a function (a reusable block of code), that variable usually only exists inside that function. If you try to use it outside the function, the program will not find it and will produce an error.

This is actually a helpful feature. It prevents you from accidentally using the wrong variable or overwriting a value you need elsewhere. As you write larger programs, understanding scope keeps your code organized and prevents bugs. For now, just know that where you create a variable affects where you can use it.

Common mistakes when working with variables

One frequent mistake is forgetting to declare a variable before using it. If you write name = "Alice" without first declaring name with let or var, some languages will create the variable for you, but others will throw an error. Always declare your variables first.

Another mistake is confusing the equals sign with a comparison. In programming, = means "put this value in this variable." If you want to check whether two values are the same, you use == or ===. Writing if (age = 25) when you meant if (age == 25) will change the value of age instead of checking it, and your program will behave unexpectedly.

A third mistake is using a variable name that is already a keyword in the language. Most languages reserve words like if, for, and function for their own use. You cannot name a variable if or for. If you try, the language will reject it with an error message telling you the name is reserved.

Frequently Asked Questions

What is the difference between declaring and assigning a variable?

Declaring a variable means telling the computer to create it and give it a name. Assigning means putting a value into it. In let age = 25; the declaration is let age and the assignment is = 25. In some languages you can declare without assigning—let age;—and the variable will exist but be empty until you assign a value later.

Can a variable hold more than one value at the same time?

A single variable holds one value at a time. However, you can create a variable that holds a list or collection of values—called an array or list—and that single variable refers to the whole collection. For example, let colors = ["red", "blue", "green"]; creates one variable that holds three text values.

What happens if I use a variable name that is already used somewhere else in my code?

It depends on scope. If the two variables are in different functions or blocks, they are separate and do not interfere. If they are in the same scope, the second declaration usually overwrites or replaces the first, which can cause bugs. Use descriptive names and avoid reusing the same name in different parts of your code.

Do I have to decide what data type a variable holds when I create it?

It depends on the language. Languages like Java and C require you to specify the type when you declare the variable. Languages like JavaScript and Python figure out the type based on what value you put in. Both approaches work; typed languages catch some errors earlier, while untyped languages are more flexible.

Can I change a variable's data type after I create it?

In languages like JavaScript and Python, yes—you can put a number in a variable and later put text in the same variable. In languages like Java, no—once you declare a variable as a number, it can only hold numbers. Keeping the same type throughout makes code easier to understand and less likely to have errors.