Contact Us
If you’ve had your interest piqued in coding in recent weeks or months, you may be keen to learn more but feel a little overwhelmed when it comes to some of the more technical language.
One term that crops up a lot is “variable”. Here we’ll explain the basics of what variables are, how they work, and why we use them.
A variable is a storage location which contains information (data), commonly referred to as a value. Variables can be manipulated by programmers and are given symbolic names (known as identifiers) to help users recognise them. Usually, this name will reference the information that’s stored in the variable, although it can be built so that this changes during the run time.
The easiest way to understand what variables are is to imagine them as the office drawers that contain the information you need. They don’t represent the information itself, but they hold the info and their structure can also influence how it is stored.
Naming variables in this way is very useful, as it allows them to be used in a whole host of programmes rather than just your own. However, naming variables can be difficult, as what makes sense to you may seem unusual to somebody else. In some cases, a value you’ve given to a variable yourself may be hard to recognise if you are revisiting months or even years later.
To help with this process, variables are typically named with simple descriptors of the information they’re holding. If a variable is storing data on costs, you would call the variable “cost”. In certain coding languages, there is an idiomatic (standard) way when naming your variables. For example, in JavaScript the standard style is known as camel case, where each subsequent word is capitalized:
const myFirstName = “Peter”
But in python, the standard way of naming variables is known as snake case, where words are lowercase and have an underscore between them:
my_first_name = “Peter”
It’s important that you research the best practice way for the language you’re writing code in!
The different types of variables depend on the programming language you are writing. As an example, in JavaScript there are only two types of variables, global and local.
Global variables are defined within the ‘global scope’ which usually means outside of any function. As soon as your script runs, the variable is declared, stored in memory and can be used anywhere in your JS code. It’s only removed from the memory once your script has finished executing.
Local variables exist only within the ‘scope’ they have been defined in. For example, if you declare a variable within a function, it will only exist in memory when that function runs. Once the function has finished executing, the variable is no longer available!
Be careful in JavaScript when you decide where to declare your variables, and try not to make too many global - as you’ll quickly begin to fill up memory. This isn’t necessarily a problem unless your application is huge! But still good practice to only use global variables when you absolutely need to.
As mentioned, there are a few other types that may pop up in various other programming languages, so be sure to carry out further research into your chosen language!
The trickiest thing with variables can be naming them (like naming anything really!), you want to make your variable names descriptive enough so that future-you or any developer you’re working with could make a reasonable guess about what information it stores. With a bit of practice it will quickly become second nature.
Marketing Manager
Joe works as the Marketing Manager at Code Nation, looking after internal and external communications and helping more people discover ways to get started in Tech. He has a background in Education and Training and has worked in Further Education for the past 10 years.

