Variables

The first step towards really understanding programming is looking back at algebra. If you remember it from school, algebra starts with writing terms such as the following.

  1. 3 + 5 = 8

You start performing calculations when you introduce an unknown, for example x below:

  1. 3 + x = 8

Shifting those around you can determine x:

  1. x = 8 - 3
  2. -> x = 5

When you introduce more than one you make your terms more flexible - you are using variables:

  1. x + y = 8

You can change the values of x and y and the formula can still be true:

  1. x = 4
  2. y = 4

or

  1. x = 3
  2. y = 5

The same is true for programming languages. In programming, variables are containers for values that change. Variables can hold all kind of values and also the results of computations. Variables have a name and a value separated by an equals sign (=). Variable names can be any letter or word, but bear in mind that there are restrictions from language to language of what you can use, as some words are reserved for other functionality.

Let’s check out how it works in Javascript, The following code defines two variables, computes the result of adding the two and defines this result as a value of a third variable.

  1. var x = 5;
  2. var y = 6;
  3. var result = x + y;