Previous Contents Next
2. Data Types and Variables

Data Types

Computer programs are processors of data. They take numbers, letters, and other special characters and manipulate them in some fashion for the purpose of producing information. They attempt, in other words, to assign meaning to bits and pieces of data by applying actions such as calculating, categorizing, arranging, presenting, or otherwise manipulating and organizing the data to derive useful information.

In JavaScript, data are recognized as one of three data types: numeric, string, and Boolean:

Numeric
Numeric data are composed of the numerals 0 - 9 plus the decimal point (.). JavaScript recognizes two kinds of numbers: integer numbers are whole numbers composed only of the decimal digits 0 - 9; floating-point numbers are composed of the decimal digits plus a decimal point. Thus, the value 12345 is an integer number and the value 123.45 is a floating-point number. JavaScript recognizes numbers when they appear in decimal notation (as above), in scientific notation (123.4E56), in hexadecimal (0XFF00CC), or in octal (012345). Numeric data are defined for the purpose of applying mathematical operations to them.

String
String data are composed of the alphabetic characters a - z and A - Z, the numerals 0 - 9, plus special characters such as "$", "#", "&", "_", and others. Strings are identified by enclosing them in quotes: "This is a string 40 characters in length". Character strings are treated as single blocks of text that can be put together (concatenated) to create a longer character string from two separate text strings or taken apart to extract a "substring" composed of a portion of the string.

Since quotes are used to surround and define text strings, a special situation arises when you want to include quotation marks as part of a text string. You can't just include them in the middle of the string--"Here is a string with "quote" marks" --since this action violates the definitional rules of a string. Instead, you must indicate that the quote mark is part of the string by marking it with a preceding backslash (\): "Here is a string with \"quote\" marks". At the same time, if you want to include a backslash character as part of a string, you must likewise precede it by a backslash (\\).

Boolean
Boolean data, which derive their name from boolean algebra, consist of two values: true and false. This data type is used in program decision making, a topic that is taken up on the next page. For now we just note its definition and two values.

Variables

In order for a computer to process data, that data must be kept somewhere within computer memory. More specifically, the computer program that processes the data must set aside memory storage areas where the numbers or strings can be placed and from which they can be accessed in order to process them. Programs create these storage areas by defining program variables.

A variable is a named memory location. Once it has been defined, it is available as a place to store data temporarily while the program is running. The program can assign data to that location, retrieve data from that location for processing, and put processing results back into that location.

In JavaScript a variable is created simply by naming it. There are, however, specific naming conventions that must be followed. Variable names

  • Must begin with an alphabetic character or the underline character

  • Can be composed of letters, numerals, or the underline character

  • Cannot contain blank spaces

  • Cannot be the same as a JavaScript reserved word. These are words that have special meanings in JavaScript and are reserved for those purposes.

Otherwise, you are free to name your variables as you wish. It makes sense, of course, to assign names that readily identify the kinds of data being stored there. For example, you can name the location used to store a social security number as "x "; however, it causes less confusion and reduces the chances of programming errors if you name it "Soc_Sec_No." Try to use names that are meaningful; you'll be glad you did when you have to trace through your program trying to find errors.

Variable names are case-sensitive. That is, JavaScript, differentiates between upper-case and lower-case letters. Therefore, variable My_Name refers to a different memory location from variable my_name. Be sure you pay careful attention to the names assigned and use them consistently.

The formal method of naming and establishing variables is to declare them with the var keyword. This word is followed by the name you want assigned to the variable, as in the following examples:

var LastName
var Soc_Sec_No
var Sales_Amount
var Todays_Date

Here, four different memory locations are set aside to contain data; plus, the kinds of data to be stored here are implied by the assigned names. Any time the JavaScript script needs to refer to or access the data, it does so through the names assigned to those locations. Thus, a script reference to LastName is a reference to whatever piece of data currently resides in that memory location. This is an important point! A reference to the name of a variable is actually a reference to the data stored in that variable.

Assigning Data to Variables

You place data into a variable by using an assignment statement. General formats for the assignment statement include:

variable = number
variable = string

The value appearing on the right of the equal size is assigned to (placed in ) the variable named on the left of the equal sign. This value can be a number (integer or floating-point) or a string (enclosed in quotes). So, for example, if you have declared the variables,

var my_number
var my_string

then you can place data into these variables with the assignments,

my_number = 12345
my_string = "a text string"

You can see the results of these statements below. Click on the radio buttons to execute the statements and watch the assignment of values to the two variables:

my_number
my_string
my_number = 12345
my_string = "a text string"

The result is that the number and the string are stored in the two variables and become accessible to your script to perform processing upon them. More accurately, the two values are accessible through reference to the variables names to which they have been assigned.

Incidentally, you can combine the two actions of declaring a variable and assigning data to it with a single statement:

var my_number = 12345
var my_string = "a text string"

And, technically, you don't have to use the var keyword. You can declare a variable and assign a value with simply,

my_number = 12345
my_string = "a text string"

Still, it's probably best to follow the formal method declaring a variable with the var keyword. Although the informal method works in most cases, there are occasions where you must use formal declarations to get your scripts to work properly. So, formality in declarations is probably the best general approach.

You'll also note in the examples above that we have used a space before and after the equal sign. You can use spaces freely in JavaScript to make your code more readable.

Assigning Variables to Variables

In the above assignment examples literal values are assigned to variables: an actual number or actual quoted string is placed in the variable. You can also assign the contents of a variable to a variable:

variable = variable

Step through the following three lines of code:

var firstString = "Hello"
var secondString = "World"
firstString = secondString

The first two lines declare two variables, firstString and secondString, and assign the strings "Hello" and "World" to them, respectively. The third line assigns the contents of variable secondString to variable firstString. Therefore, at this point, both variables contain the string "World".

Three main points need to be made about this assignment operation. First, references to variables are references to their contents (a point made earlier). Second, an assignment from one variable to another does not remove the contents of the assigning variable. The data contained in the variable on the right (secondString) is copied to, or duplicated in, the variable on the left (firstString). Third, the contents of the assigned-to variable is replaced by the contents of the assigning variable. The data contained in the variable on the left ("Hello") is replaced by the data contained in the variable on the right ("World").

You can see these actions take place below. Click the radio buttons, in order, to execute the script and view the contents of the two variables:

firstString
secondString
var firstString="Hello"
var secondString="World"
firstString = secondString

In summary, then, JavaScript scripting requires you to define variables to establish memory storage locations for holding data. Once stored in these variables, the data--either numeric or string-- become available for processing, with the results stored in these and other variables. Of course, if all you could do with a scripting language is to assign literal values to variables and then simply move them around from one variable to another, there wouldn't be much use for the language. The real power in a language is in its numeric and string processing features--the ability to perform arithmetic and other mathematical processing on numbers and to perform "put together" and "take apart" functions on strings. This processing is taken up beginning on the next page.


Previous Contents Next