Tech Term: Variables

Variables! Those magical little treasure chests that port our data all around.

You will remember variables from your high school math days. You probably had to solve equations something like this one:

x + 5 = 7

What is the value of the variable x? (2, of course!)

In math, variables served as placeholders, things to be resolved, cracked open, figured out and often, immediately dispensed with.

In programming variables can work like placeholders, but they serve a more important function: to serve as capsules that store pieces of data and port that data through your scripts. You can think of variables like little treasure chests where you put your most valuable data and send it out into your big bad script.

In order to use a variable you have to first create it and give it a name, and then you have to put something inside of it.

Each programming language has a slightly different way to do this.

So let’s say you want to create a variable for your name.

In JavaScript you might write:

var myName = "Rachel"

In Ruby it would look like this:

myname = "Rachel"

And in PHP, more like this:

$myName = "Rachel"

There is no meaning behind the differences between languages, you just have memorize how to create a variable in each one.

Sure, sure, sounds good. I’ve create a variable treasure chest called myname and stuck my actual name inside of it, but now, what do I do with it?

Well, now that your program knows your name, there are tons of possibilities!

For example, you could write a simple script to translate your name into pig latin.

NOTE: This script is written in pseudo code and can not actually be run.

myname = “Rachel”
How to translate myname into pig latin:
1. If the first letter of myname is equal to a vowel (a, e, i, o, u),
then add “way” to the end of myname
2. If not, then take the first letter, stick it on the end of the name,
and add “ay” to the end
3. All done!
Now, go ahead and translate myname!
Result => "achelRay"

And look! Just like that you wrote yourself a program that translates your name into pig latin.

The beauty of this script is that because you used a variable (instead of just “Rachel”), you can easily swap out your name for someone else’s (like Albert’s) and the program will still work.

Variables are, after all, variable!

myname = "Albert"
Please translate myname
Result => “Albertway”

Ah ha! What else can I put into this magical variable treasure chest?

Well, really, anything. You already know that you can put in strings (aka little bits of text that look like this: “this is a string,” quotation marks required). You can also put numbers into the treasure chest (often called Integers), arrays (collections of multiple pieces of data), and even other variables.

If it’s an object in the world of computers, then you can stick it in a variable.

Cocktail Party Fact

One of the challenges of variables is that they are, as we mentioned, variable.

Oftentimes when you are writing a program, your variable will be changing and changing and it’s hard to keep track of what it is at any given moment (as in, does myname still mean “Rachel” or did I change it to “Albert” or “Thomas” ??)

For that reason, when programmers are debugging their programs you will often see them using all kinds of funny methods to make sure that they know the value of a variable at any given time.

In JavaScript you will often see programmers use the alert or console.log methods, like so:


In Ruby, you can use the print, puts, or inspect methods:

puts myname
print myname

In PHP echo is often the way to go:

echo $myname;