A variable can be thought of as a named location in the computer's memory where a value is stored. You can think of the name (or identifier) as a sort of label for the memory location. For example, a variable called sum might have the number value 3 stored in it. We say that the variable named sum has the value 3, or is assigned the value 3.
myVar = 3;
In the first line, var is a reserved word (also known as a keyword). The line
is the declaration of a variable with name myVar, i.e. the program is told to use myVar as the name for a location in the computer's memory where some data will be stored. With this line alone, myVar doesn't yet have a value.
The second line,
myVar = 3;
assigns a value of 3 to myVar, i.e. puts the value 3 in the memory location labelled myVar.
The third line,
writes the current value of myVar into the browser's current page. Notice that myVar is not enclosed in quotes, as we want to display the value of the variable called myVar, in this case 3, not the text 'myVar'.
Question: Suppose we have a variable called total, which is assigned the value 5.
total = 5;
(a) The word total will be displayed in the browser's current page.
(b) The number 5 (the value stored at the memory location labelled total) will be displayed in the browser's current page.
The semicolon and sequential execution
Sequential execution means carrying out one instruction, and the next, and then the next, and so on until all the instructions have been executed. All the programs we'll be examining execute in this way.
myVar = 3;
1. Open the file in your browser to remind yourself what it does.
Save your changes and open the file in your browser again to check that your change has the desired effect.
1. You will see that the output is the current value of the variable, myVar, which is 3.
2.Changing myVar = 3 to myVar = 145 will achieve the required change.
myVar = 6;
yourVar = myVar + 3;
myVar = yourVar;
(a) myVar = 3;
(b) yourVar = myVar + 2;
(c) myVar = yourVar;
(a) The value of myVar is 3; we don't yet know anything about the value of yourVar.
(b) The value of myVar is still 3; the value of yourVar is the value of myVar plus 2, i.e. 5.
(c) The value of myVar is the value of yourVar, i.e. 5; the value of yourVar is still 5. (But note that there are still two seperate memory locations; they just happen to hold the same value.)
Variables are often compared to named boxes or pigeon-holes. Although this helps us to visualise what's happening in the computer, the comparison is not entirely appropriate, as we can normally add a number of items to a pigeon-hole whereas a variable can only hold one value at a time. If a new value is assigned to the variable, then this value overwrites the previous value, which is lost.
(a) What are the two stages in executing the following instruction (where yourVar is a variable)?
yourVar = yourVar + 3;
(b) If the value of yourVar before execution of the statement is 10, what will its value be after execution of the statement?
(a) First of all, the expression on the right-hand side will be evaluated, i.e. 3 will be added to the current value of yourVar. Then this new value will be assigned to yourVar.
(b) Its value will be 13 (the result of 10 + 3).
What is the value of the variable myVar after both the following lines have been executed?
myVar = 21;
myVar = myVar - 5;
The value of myVar is the value assigned to myVar in the first line (21) minus 5, i.e. 16.
- The first character must be one of the following:
- an upper-or lower-case letter (that is, A...Z, a...z);
- an underscore character (_);
- a dollar sign ($).
- Subsequent characters may be any of the above or a digit (0, 1, 2, ..., 9).
The above rules governing identifiers are mandatory. However, we encourage you to respect the following style guidelines when choosing identifiers for your variables. Sticking to these guidelines will make your programs easier to read and (if necessary) debug.
- Although there is no lower or upper limit on the number of characters that can be used in an identifier, we would urge you to avoid very short identifiers, such as a, b, x, ch, because they are not very informative.
- As far as possible, avoid the use of $ and _ in identifiers. Identifiers composed entirely of letters are usually the most meaningful to human readers, though it might sometimes be appropriate to use digits in identifiers for a number of similar values, e.g. sum1, sum2, sum3.
- Choose meaningful names, i.e. names which give some indication of the role played by the variable.
- Start your identifiers with lower-case letters. Where an identifier is composed of two or more English words, then we suggest the use of a single upper-case letter to mark the start of each word after the first. We have used this style of identifier (known as camel-backed identifiers) for myVar and yourVar. Here are a few more examples: totalCost, dateToday, myFamilyName.
123 is invalid – an identifier must start with a letter, _ or $.
$sum and PriceAsked are both valid, though they don't conform to our style guidelines!
Given that programming languages were designed primarily as a way for people to communicate with computers, why should programs be easy for people to read, as well as to write?
One reason is for ease of debugging: if your program doesn't run at all, or if it doesn't run as you expect, you need to be able to find your mistake. This involves reading the program code and understanding what it does. Another reason is that you may wish to modify an existing program, rather than write one from scratch, in order to solve a particular problem or to carry out a particular task. Again, it is important that you understand what the existing program does. You may have thought of other reasons.
Open up the following file in your preferred text editor - variables_activity_2.html. You'll notice it's very similar to the version we used previously.
Delete the line
myVar = 3;
Save your changes and open the program in your browser.
You will get the word undefined displayed in your browser's current page. This is because the variable myVar has not had any value assigned to it.
The process of assigning an initial value to a variable is called initialisation – until you initialise a variable it will not have a value.
If you want to try something a little more complicated, try editing this code to do the following:
- Name your variable myNumber, or another new similar, valid variable name
- Initialise your variable with the number 7
- Add 3 to your variable's value
- Display the value of your variable on the screen.
If you have trouble with this, click below to view the correct code. Try to think of where you might have gone wrong first though. Remember your code is executed sequentially!
myNumber = 7;
myNumber = myNumber + 3;
Some useful data types
Some of the data values that we have seen so far are recognisably of the same kind, or type. For example, the values 3, 7, and 21 are all of the number type, as indeed are 3.24, –6 and –2.5.
Hopefully now you should have a good understanding of what a variable is and an idea of some of the ways that they can be used.When you're ready to continue, pick your next lesson from the menu below. We recommend 'Operators' next.