A variable is a designated container for storing a piece of data or a value, to which we give a name in order to access, manipulate, and reuse it throughout the program’s execution.
Our program will have to manage data. In fact, that is its main function, to manipulate and manage data. And, somewhere we have to store that data. That place is variables.
It is often said that a variable is “like a box” where we store data. It’s quite appropriate and useful for explaining the concept, so here comes this box!

For example, we can have a variable called “age” that stores a person’s age. The first thing we see is that we have given our box a name, to be able to find it and differentiate it from others.
Now I take my “age” box and place the number 25 inside. And we have another property of variables, which is that they serve to store things. Variables can contain different types of information, such as numbers, names, colors, or anything we need in our program.
If at any point I want to check the age I saved, I simply open the box and get the number 25.

The advantage of variables is that their content can change. But we don’t need to change the box itself; our “age” box remains the same. We are simply going to change what it contains.

So in a year, when the person has a birthday, we can open the “age” box, change the number to 26, and close it again. Now, when asking the “age” box, it will give us the updated number 26.
In summary:
- Variables contain things (information)
- They have a name to differentiate them from others
- The content can change, without needing to change the variable itself
Content of a Variable
A variable is called that because its content can vary. I know, it’s a bit obvious to say a variable varies, but that’s how it is (in contrast, things that cannot vary are called constants).
The value we can store can be anything the language allows. From a number, to a text string, or a more complex object like the complete list of orders from a factory.
The variable has a name and an associated data type, which allows it to be identified and its content accessed at runtime.
For example, some code
myText = "an impressive text"
myNumber = 42
Here,
miTextois a variable storing the text “un texto impresionante”miNumerois a variable storing the number 42
Depending on the language we are using, we will have certain types of variables available. On the other hand, there are languages with strict typing and languages with dynamic typing.
Declaration of a Variable
In many programming languages a variable must be declared before using it. This means telling the compiler or interpreter “hey, I want to create this container with this name!”
Additionally, in typed languages we must specify the type of data the variable can store.
For example, in C# (strict typing) to create a variable of text type we would do.
string myText = "an impressive text";
In JavaScript (dynamic typing) we need to declare the variable, but it’s not necessary to indicate its type. It would look like this.
let myText = "an impressive text";
On the other hand, in Python it is not necessary to declare the variable nor indicate its type. So the syntax would be like this,
myText = "an impressive text"
Assignment of a Variable
To change the content of a variable throughout the program, we simply have to assign it a new value. Here there is a consensus, and in almost all languages the = operator is used.
Therefore, when the variable already exists, in C# to change its value we would do.
myText = "another even more impressive text";
Which would be identical to the case of JavaScript
myText = "another even more impressive text";
And even to that of Python,
myText = "another even more impressive text";
However, it should be said that if, for example, we made a mistake writing the variable name and wrote miText instead of miTexto
myText = "another even more impressive text";
In C#, JavaScript, and in most languages, it would give us an error because that variable doesn’t exist (we haven’t declared it). However, in Python a new variable called miText would be created, without any warning.
As we see, allowing variables not to be declared can be both an advantage, because it’s very convenient, and a possible source of errors.
Assigning a Variable to Itself
During variable assignment it is possible to use its own content to calculate the new value. For example, it is valid and very common to do the following,
counter = counter + 1
With that statement we are incrementing the counter by one.
This is because the evaluation of that expression happens in two steps,
- The expression on the right is evaluated
- The resulting value is assigned to the variable on the left.
There is no problem if the expression on the right contains the variable itself. It won’t cause an infinite loop, crash your computer, or anything. It simply evaluates the right side first, and then stores it in the variable on the left.
