Introduction to programming

Variables

Now on to the interesting stuff! We'll now have a look at the programming basics that exist in pretty much every programming language, in one form or another. Before starting this section, make a new copy of the template.html file and open it in your browser and text editor.

Variable basics

Variables are containers that allow you to store something that you want to use later on, be that a simple name or age value, or something more complicated. Why would you want to do this? Let's look at a simple program that says hello to you: See my Hello example (don't look at the source code!)

Without variables, it would be impossible to create a personalised hello message. You'd just have to stick to "Hello there.", which would be really boring. So variables are all about allowing us to create programs that dynamically change depending on things that differ as the program is being used, such as the user using the program, the date and time of day, the weather, and so on. Programming is pretty much impossible without variables.

To use a variable, we first have to declare it — we say "I am making this variable exist. Its name is x, and its initial value is y."

A really simple code example:

var myHairColor = "brown";
// Note the quotes; these can be single or double quotes;
//your choice, as long as you are consistent

You could also then change a variable later on, once it is declared.

var myHairColor = "brown";
// declare a variable myHairColor, with an initial value of "brown"

myHairColor = "red";
// change the existing variable myHairColor's value to "red"

A few more simple examples follow:

var myName = "Chris";
// A string
// Also note that JavaScript is case sensitive, so myname is
// a different variable to myName

var myAge = 35; // a number
var haveKids = true; // a boolean
var favouriteThings = ['raindrops','kettles','packages','ponies'] // an array

Exercise: Proving how variables exist and are stored

As a quick exercise, let's go into the browser developer console and try accessing a new variable the console pane (the console is basically a way to access the internal state of the code or application). To open the console, press Ctrl + Shift + I; Cmd + Opt + I on Mac OSX; F12 on Internet Explorer, and click the console tab.

To call or access a variable, you just use the variable name. Enter this into the console:

myCar

This should give you a result of undefined, because the variable doesn't yet exist.

Let's try declaring the variable: type the following into the console:

var myCar = "Citroen";

and then trying this again in the console:

myCar

The console should now return the variable's value to you.

When we have a value stored in a variable, we can use it in our program by simply saying the variable name again. Let's look at something slightly more complex that actually does something interesting. The prompt function opens up a dialog box asking the user to enter a value into a text box. When they have done so, it stores that value in a variable:

var myHairColor = prompt("Enter your hair color");
// Ask the user for their hair color

You can then choose to do something else with that value:

alert("Chris has " + myHairColor + " hair.");
// display a personalised message about hair colour

alert is a simple function that pings up a dialog box containing whatever message you include inside it, in this case Chris has " + myHairColor + " hair.".

This string of text may look strange, and we'll explain it better later on. For now, let's just say that we are gluing together three things:

  1. The text "Chris has "
  2. The current value inside the myHairColor variable
  3. The text " hair."

A lot of programming involves gluing different things together to get the output you want.

Exercise: write the hello example from before

The hello example that we checked out at the very start of this section is very similar in the way that it works to the hair colour example we just studied. I'd like you to try writing the hello example into your working template file, and then run it to get it working. DON'T look at the source code unless you get stuck!

Variable types

In JavaScript, different variables have different assigned types: