Shawn Decker - ATS 3137
School of the Art Institute of Chicago
Javascript/Actionscript: Data Types and Values
Following are the kinds of data that you can store in Javascript and Actionscript. The three basic types of information we have already mentioned: numers, character strings, and Boolean logical values are listed first.
Two other data types that are new: the array and the object are introduced here as well.
The following is generally true of both Javascript and Actionscript. In the text below, when referring to Javascript, I mean BOTH Javascript and Actionscript.
Numbers
Numbers are the most basic data type that there is. Javascript differs from other languages in that it does not make a distinction between floating point values (values with a decimal) and integer values. All numbers in Javascript are represented as floating point values. When a numer apperas directly in a Javascript program, this is called a numeric literal. Types of numeric literals which are supported in Javascript include:
Type Examples
Integer Literals 5
0
1000Octal Literal (always begins with a 0) 05
0300Hexadecimal Literal (always begins with 0x) 0xF7
0x445CFloating Point Lliteral (always contains a decimal point) 33.76
0.700374
Character Strings
A string is any sequence of characters surrounded by either single or double quotes. Please make sure that you are not using "smart quotes" - these won't work. If it is ever (and it is) necessary to include a quoted string within another quoted string, use the double quotes for the outer string, and the single quotes for the embedded string. An example:
"Sometimes it is necessary to 'place another string' inside of a string."
There are some special characters it is good to know about that may be used inside of strings.
these include:
Sequence Character \b Backspace
\n
Newline \r Carriage return \t Tab \" Double Quote \' Single Quote \\ Backslash \xXX latin-1 character specified by the two hexadecimal digits XX.
Boolean values
Unlike the other types of data types, Boolean values only allow two types: true and false. Boolean values are generally the result of comparisons made within your program (i.e. "i< 5"). Literals used to assign Boolean values are simply "false" and "true". As an example which creates the variable "isOn" with one of these values:
var isOn = false;
or
var isOn = true;
Objects
An object is a collection of related pieces of data. We will discuss Objects in much more detail soon. For now, it is enough to know that Objects are essentially a collection of "properties" which are each a named value. To refer to a specific property of an object, we use what is often called "dot" syntax. The object name is followed by a period and then by the spefic property name. For example, browsers define a number of objects which contain all the properties of each item displayed on the page. The image object, for example has a number of properties, including the height and width of the image. These would be accessed as:
image.height
image.width
Objects can also act like what are called "associative arrays" in other languages. If this doesn't mean anything to you - just skip it, we will talk about it later. For those who know what an associative array is, the above example can also be done as:
image["height"]
image["width"]
In both actionscript and javascript, properties of objects can be accessed either using the "dot" syntax, or by accessing the property via an associative array. Anexample of this equivalence would be:
myMovie = new MovieClip; // create a new movie clip called "myMovie"
trace(myMovie._x ); // MyMovie's x location on the stage
trace(myMovie[_x]; // The same using an associative array
Arrays
An array is also a collection of individual data values, just like an object. Each of these values in an object is named (as above), but in an array they are numbered. Each element of an array is referenced by the array name followed by the number of the element in square braces. Numbering starts at 0. Arrays are created with the command "new Array()". Unlike other languages, arrays can contain various data types, and also they are of variable length. An example of an array is:
a = new Array(); a[0] = 29; /* puts the number 29 in the array's first location */ a[1] = 34; /* puts the number 34 in the array's second location */ a[2] = "a text string"; /* puts the character string into the 3rd location */ a[3] = a[0]; /* copies the value from the first location into the 4th location */
An associative array is another form of array which is very useful in javascript. In this format, any object may be treated as an array, where the name of the property in question is used (this MUST be given as a character string) in place of the index number. Thus, for a document with an image object named "bicycle", there are two ways to refer to this object's .src property:
standard dot syntax:
document.bicycle.src
or, as an associative array:
document["bicycle"].src