Skip to content
Blog

What is a variable? A beginner's guide to programming

|
programmingbeginnervariables

What is a variable?

If you have just started learning programming, variables are one of the very first things you encounter. But what actually is a variable — and why is it so important?

In short: A variable is a named container that stores data in your program. Think of it as a label on a box — the box contains a value, and the label gives you the ability to find it again.

Why do we need variables?

Without variables, programs would not be able to remember anything. Imagine a calculator that cannot save intermediate results — it would be useless.

With variables, your program can:

  • Store user input — e.g. a name or an age
  • Keep track of state — is the user logged in? How many points do they have?
  • Reuse values — calculate something once and use the result in multiple places

Variables in practice

Let's see how variables look in three popular programming languages.

C#

string navn = "Kasper"; int alder = 25; bool erStuderende = true; Console.WriteLine($"Hej, jeg hedder {navn} og er {alder} år gammel.");

In C# you must specify the type of the variable (string, int, bool). This is called static typing — the compiler checks that you use the variable correctly.

Python

navn = "Kasper" alder = 25 er_studerende = True print(f"Hej, jeg hedder {navn} og er {alder} år gammel.")

In Python you do not need to specify the type — the language figures it out on its own. This makes the code shorter, but you lose some of the safety that typing provides.

JavaScript

let navn = "Kasper"; let alder = 25; let erStuderende = true; console.log(`Hej, jeg hedder ${navn} og er ${alder} år gammel.`);

JavaScript uses let (or const for values that do not change) to declare variables.

The most common data types

Regardless of which language you use, you will encounter these fundamental types:

TypeDescriptionExample
StringText"Hej verden"
IntegerWhole number42
Float/DoubleDecimal number3.14
BooleanTrue/falsetrue / false

Naming variables

Good naming is surprisingly important. Compare:

// Dårligt — hvad betyder x og y? int x = 120; int y = 60; // Godt — klart og beskrivende int bruttoPris = 120; int rabat = 60;

Rules for variable names:

  1. Start with a letter (or underscore) — not a number
  2. No spaces — use camelCase (myVariable) or snake_case (my_variable)
  3. Use descriptive namesage is better than a
  4. Avoid reserved words — you cannot name a variable class or return

Variable vs. constant

Sometimes you have a value that should never change — like pi or the VAT rate. Here you use a constant:

const double Moms = 0.25; const double Pi = 3.14159;

A constant is like a variable, but it is "locked" after it is set. If you try to change it, the compiler gives an error.

What is the next step?

Now that you understand variables, you are ready to learn about control structures like if-statements and loops. They use variables to determine what your program should do.

Want to try it in practice? In our C# course you start writing real code from the first lesson — including variables, types and Console.WriteLine().

Ready to try it yourself?

Learn to code with interactive exercises and instant feedback — right in the browser.