Types of Variables



Names can be given to variables, but the manner in which they appear or are used may differ. These differences are reflected in the way each may be programmed. Perhaps the easiest way to note the differences is to visualize how each type may appear on a sheet of paper.

The simplest type is simply an isolated number or word. A variable is something that is put in a storage location, so it doesn't matter whether that something is a number or a word. An isolated variable might be, in a particular instance, the number of people attending a social, or it might be the title given to the social. In either instance, to specify just one of these would require a solitary storage area. This may be represented as is shown below:

npeople
social


Here the number of people at the social is represented by the variable name "npeople" and the title by "social". Every occasion may have a different title or a different number of people in attendance, but this representation will apply for all of them. It is only what is in the storage areas that changes. Note that the name chosen for the event was "social" and not "function", as "function" is a reserved JS word.

The people attending a function would normally have their names listed in a vertical manner, while the table number where they are seated may be listed horizontally.

name(1)
name(2)
name(3)
name(4)
name(5)
name(6)
table(1)table(2)table(3)


These are examples of indexed variables, where the name for each variable is the same but its value in a location is identified by its numeric rank, which appears in parentheses. This storage representation is called a linear array. A particular value can be obtained by specifying the name and the location number, just as with a telephone or a calculator.

Another kind of array is a tabular array. This may be represented by a table of the costs incurred by each person at a particular table seating. No one at a particular table, then the "cost" incurred is 0. This kind of array is illustrated below:

 
table(1) table(2) table(3)
name(1)
name(2)
name(3)
name(4)
name(5)
name(6)
cost(1,1)cost(1,2)cost(1,3)
cost(2,1)cost(2,2)cost(2,3)
cost(3,1)cost(3,2)cost(3,3)
cost(4,1)cost(4,2)cost(4,3)
cost(5,1)cost(5,2)cost(5,3)
cost(6,1)cost(6,2)cost(6,3)


In this instance two indices must be used to gain the value in a particular location: one identifying the person and the other the table.

There is yet a third type of an array, but this is not as usual. For this one envision that there is a tabular array, such as that given above, for each social that may occur over, say a week or a month. Each of these may be viewed as a page, a "record" of the social, and a sequence of them together a book. To gain a value in a particular location three indices are now needed: the page number, p, the the number for the person on a given row, r, on that page, and where that person was seated as shown by the table column number, c. These three types of variables can be written in a general manner:

socialname(r) and/or table(c)cost(r,c)
record(p,r,c)


Though each type of variable is written differently, as long as the correct integers are used, gaining a value is no different from recalling a number from the memory of a telephone or a calculator. It is just done automatically now rather than manually.

Older browsers or browsers using JS1.0 do not have an Array object, i.e., a built-in array storage, whereas browsers using JS1.1, as does WebTV, have an "Array" object. In fact all JS objects are stored in an array. Thus, to store a single number in each type of variable storage the coding could be written:

npeople=30;name(24)="Smith, J. J.";


cost(5, 2)=65.78;record(38, 5, 2)=154.25;


In creating array storage the browser must be "told" that an array object will be used in the coding that follows. There are two ways to do this: implied or explicit. In the first the length of, say, a linear array is not specified while in the second it is:

Implicit
names=new Array();

name[0]="Segal, M.";
name[1]="Jones, D.";
name[2]="Svenson, I.";
name[3]="DeLicioso, A.";


Explicit
var npeople=6;

table=new initArray(npeople);

table[0]=1;
table[1]=2;
table[2]=3;


New Array() sets aside a block of storage for an array. Init Array(npeople) does the same, but specifies how many storage locations. Stating the maximum number at the outset can be useful later in the coding. In a way it does not matter how many locations are set aside, because arrays can always be made to grow. They cannot be shortened though, since it is a built in storage area. It should be noted that the location number always starts with 0 and not 1. This is a JS convention. Note also, parentheses, ( ), for array elements are replaced by square brackets, [ ]. This too is a JS convention.

<< PreviousNext >>