Types of Data

In JScript, the data contained in a variable can be one of six types: string, number, Boolean, null, undefined, and object. Each data type is suited for a specific purpose. Unlike most programming languages, JScript is not strongly typed—that is, in JScript the same variable can contain one type of data, such as a string, at one time and another type, such as a number, at a later time. The sections that follow take a closer look at each type of data.

String Data Type

A string is a series of characters, which can include letters, numbers, and symbols. A string is separated from the surrounding code with either single or double quotation marks. For instance, the following lines of code are equivalent; each assigns the same value to a variable named myVar:

 myVar="This is a string" myVar=`This is a string' 

NOTE
Let's define some basic terminology: a character is any letter, number, or symbol; a letter is any member of the alphabet (uppercase or lowercase); a number is one or more digits; and a symbol is any nonletter, nonnumber character, such as a punctuation mark.

Strings in JScript are made up of unicode characters. Unicode is a standard that defines thousands of characters from many languages. A string can be millions of characters long—although any script using such a string probably would perform very slowly.

If you want to include either single or double quotation marks as part of the string itself, you can simply use the other set of quotation marks to define the string. For example, the following line of code assigns the string value He said, "Hello!" to the variable myVar:

 myVar=`He said, "Hello!"` 

You can also represent quotation marks and certain other characters by using escape sequences. An escape sequence is simply a special code for a character. Escape sequences are generally used for two purposes:

  • To signify that a character normally used in the context of program code is instead being used as part of a string (for instance, when you want double quotation marks to appear as part of a string even though the string itself is enclosed in double quotation marks)
  • To include control sequences that do not usually have character equivalents, such as a carriage return

Escape sequences consist of a character preceded by a backslash. Here are the characters for which JScript provides string escape sequences.

Character Escape Sequence
single quotation mark \'
double quotation mark \"
backslash \\
backspace \b
carriage return \r
form feed \f
new line (linefeed) \n
tab \t

The following line of code demonstrates some of these escape sequences:

 myVar="Here are some special characters - \\,\",\'." 

In this line, the variable myVar is assigned the string Here are some special characters - \, ", `.

NOTE
In addition to supporting escape sequences in JScript, browsers can display foreign language letters, special characters, and symbols through the use of character entities. For instance, the character entity   represents a nonbreaking space. Find out more about character entities on the SBN Workshop Web site and on the CD that accompanies this book. On the CD see Workshop (References); DHTML, HTML & CSS; HTML References; Character Sets. To get to the online version, visit the MSDN Online Resource page at http://msdn.microsoft.com/resources/schurmandhtml.htm, or open the file named MSDN_OL.htm on the companion CD, and choose Character Sets.

Number Data Type

You can also represent data in JScript as an integer (a whole number, such as 2) or as a floating-point number (a number containing a decimal point, such as 54.686). These numbers are normally shown in base 10 (standard decimal numbers), but they can also be represented in scientific notation, in base 8 (octal), or in base 16 (hexadecimal). The following lines of script demonstrate these different ways of expressing numbers:

 varInDecimal=46 varInScientific=4.6e1 varInOctal=056 varInHexadecimal=0x2e 

Each of these variables is assigned the same value, 46. (You can find a full explanation of each type of number at the Microsoft Developer Network (MSDN). To get there, visit the MSDN Online Resource page at msdn.microsoft.com/resources/schurmandhtml.htm, or open the file named MSDN_OL.htm on the companion CD, and choose JScript Data Types.)

Many languages have several distinct data types to represent numbers. In these other languages, you must specify whether a variable is to be of integer type or floating-point type, and thereafter you can use that variable only for that type of numeric data. JScript, however, provides only one number data type that allows both types of numeric data. In fact, a single variable can contain an integer value at one time and a floating-point value at another.

In addition to numeric values, there are several special values available for variables of type number, as shown in Table 8-1.

Table 8-1 Special Values for Variables of Type Number

Variable Value
Infinity Infinity. Treated as positive infinity for mathematical operations.
+Infinity Explicit Positive Infinity. Treated as positive infinity for mathematical operations.
-Infinity Negative Infinity. Treated as negative infinity for mathematical operations.
+0 Positive Zero. Assigns positivity to this normally signless value.
-0 Negative Zero. Assigns negativity to this normally signless value.
NaN Not a Number. This value can be assigned, or it can be the result of mathematical operations. For example, -Infinity divided by +Infinity results in NaN.

Boolean Data Type

Boolean data can have one of only two possible values, true or false. Unlike some other languages, such as many versions of C and C++, in which Boolean data types are simulated, using 0 for false and 1 or nonzero for true, JScript provides a genuine Boolean data type. Boolean data is commonly used to perform tests that determine whether an action will take place. For example, you can create a conditional statement that will perform one action if a Boolean value is true and another if it is false. Chapter 9 discusses the use of Boolean values with conditional statements.

Null Data Type

The null data type is actually both a value and a data type. Setting a variable to null explicitly states that the variable has no value. It also specifies that the variable is not one of the other data types, such as a string or a number. The following line of code assigns a null value to the variable myVar:

 myVar=null 

Notice that although null is a data type in JScript, this is not the case in some other languages where null is simply equivalent to 0. This data type is frequently used in error handling, as is undefined.

Undefined Data Type

The undefined data type is a special type that cannot be assigned to a variable. Instead, a variable is considered to be of the undefined type if it has not been declared, or if it has been declared but does not have a value. You will frequently see an error message saying that a variable is undefined if you try to use a variable that has not been previously set to a value. For instance, the line var1=var2 results in the error `var2' is undefined if var2 has not been previously defined and set to a value.

Code Listing 8-1 demonstrates the data types we've discussed so far. Figure 8-1 shows the display that results when the browser processes this code.

Code Listing 8-1.

 <HTML> <HEAD> <TITLE>Listing 8-1</TITLE> <SCRIPT LANGUAGE="JavaScript"> var a="\"word\"" var b=32 var c=true var d=null var e document.write("a="+a+", b="+b+", c="+c+", d="+d+", and e="+e) </SCRIPT> </HEAD> </HTML> 

click to view at full size.

Figure 8-1. Displaying different data types.

In Code Listing 8-1 and Figure 8-1, variable a contains a string value, b a number value, c a Boolean value, d is set to null, and e is not given a value. Notice the plus signs in the line of code that begins with document.write. These plus signs join the different strings on the line to create one large string, which is then written to the screen as HTML by the document.write() method. (Later in this chapter, we will discuss using plus signs and other operators.)

Object Data Type

The final data type supported by JScript is object. This category is wide and varied. For example, there is an array object that allows you to group pieces of data and a date object that is useful for storing dates and times. You can also construct your own custom objects as well as assign Object Model objects to JScript variables. Code Listing 8-2 provides a demonstration of assigning an object in the Object Model to a variable. Figure 8-2 shows the resulting display.

Code Listing 8-2.

 <HTML> <HEAD> <TITLE>Listing 8-2</TITLE> </HEAD> <BODY> Here is a text input. <FORM NAME="F1"> <INPUT NAME="T1" TYPE="text" VALUE="foo"> <SCRIPT LANGUAGE="JavaScript">   a=document.forms[0].elements[0] </SCRIPT> <INPUT TYPE="button" VALUE="Display Value" onclick="alert(a.value)"> </FORM> </BODY> </HTML> 

click to view at full size.

Figure 8-2. Accessing the Object Model. Clicking the Display Value button displays the value of the text input.

The script shown in Code Listing 8-2 contains a form that, in turn, contains a text box. This text box is represented as an object in the Object Model. We assign the text box object to the JavaScript variable a with the code a=document.forms[0].elements[0]. Thereafter, we can refer to the text box simply as a, which is not only easier to type and understand but can also result in substantial speed increases in the script if the object is referred to multiple times. Consequently, the code a.value now refers to the value property of the text box object—and when the Display Value button is pressed, the browser displays the value of the text box's value property. Also notice that you can change the contents of the text box; click the Display Value button again, and the browser displays a dialog box containing updated contents of the text box.

You can create your own JScript objects using the new operator. Like the built-in objects in the Object Model, these objects can have their own properties and methods. In Code Listing 8-3, an object is created and properties are assigned to it. You can see the result in Figure 8-3.

NOTE
The Document object has a collection called forms, which refers to all of the forms in the current document. Each item in this collection, in turn, has a collection called elements, which simply represents all of the elements (INPUTs, SELECTs, and so on) in that form. The object name document.forms[0].elements[0] is the most generic and cross browser/platform/version way of referring to the input in our example. If we were developing only for Internet Explorer 4 or later, we could simply use the expression a=F1.T1, because of the Internet Explorer enhanced object model.

Code Listing 8-3.

 <HTML> <HEAD> <TITLE>Listing 8-3</TITLE> <SCRIPT LANGUAGE="JavaScript"> var Stu1=new Object Stu1.name="John Doe" Stu1.test1="B+" Stu1.test2="A-" document.write("There is an object called Stu1<BR>") document.write("Its name property equals "+Stu1.name+"<BR>") document.write(Stu1.name+"`s score on test1: "+Stu1.test1+"<BR>") document.write(Stu1.name+"`s score on test2: "+Stu1.test2+"<BR>") </SCRIPT> </HEAD> </HTML> 

click to view at full size.

Figure 8-3. Creating an object.

With the code var Stu1=new Object in Code Listing 8-3, an object named Stu1 is created. We then assigned values to some new properties of the Stu1 object, name, test1, and test2. Thereafter, we can access these values by referring to these properties of the Stu1 object.

Constructor Functions

JScript supports the use of constructor functions, which can be used to easily create large groups of objects that you define. The following snippet of code results in a constructor function used to create the type of object used in Code Listing 8-3.

 function makeStudent(strName, strTest1, strTest2){   this.name=strName   this.Test1=strTest1   this.Test2=strTest2 } 

The object itself can then be created with the following line of code:

 Stu1=new makeStudent(`John Doe','B+','A-') 

Additional objects can be created easily, as follows:

 Stu2=new makeStudent(`Jim Smith','B','B-') Stu3=new makeStudent(`Lori James','B-','C-') Stu3=new makeStudent(`Jane Doe','A+','A') 

Array Objects

An array object can be thought of as a numbered list of associated pieces of data. Each piece of data is an element of the array and has an associated index number. Arrays are zero-ordered (numbered starting with 0). You can refer to a particular element in an array by using the name of the array and the index number of the element enclosed in brackets. Thus, in an array named myArray, you refer to the first item in the array as myArray[0]; the second item is myArray[1]. You can create an array with the new operator, as we do in Code Listing 8-4, and then populate it with data. The results are displayed in Figure 8-4.

Code Listing 8-4.

 <HTML> <HEAD> <TITLE>Listing 8-4</TITLE> <SCRIPT LANGUAGE="JavaScript"> var nameList=new Array nameList[0]="John" nameList[1]="Mary" nameList[49]=3774 document.write("The first value is "+nameList[0]+"<BR>") i=49 document.write("The value at index "+i+" is "+nameList[i]+"<BR>") i=25 document.write("The value at index "+i+" is "+nameList[i]+"<BR>") </SCRIPT> </HEAD> </HTML> 

click to view at full size.

Figure 8-4. Using an array.

In JScript, each element of an array can contain a different type of data. Note that the array in Listing 8-4 contains both strings and a number. In addition, an array does not need to contain an element at every index location. This is why we can skip from index 1 to index 49 in Code Listing 8-4. Elements that are skipped have a value of undefined.

NOTE
Arrays and objects are similar in JScript. In fact, you can name the indexes of an array with words rather than with numbers—for instance, nameList["name1"]="John". From then on, you can refer to this element either with array notation (nameList["name1"]) or with the standard dot notation commonly used with objects (nameList.name1).



Dynamic HTML in Action
Dynamic HTML in Action
ISBN: 0735605637
EAN: 2147483647
Year: 1999
Pages: 128
Authors: Eric M Schurman, William J Pardi
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net