NOTE: Because JavaScript is case sensitive,This relatively small set of types of values, or data types, enables you to perform useful functions with your applications. There is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type in Navigator. However, you can use thenull
is not the same asNull
,NULL
, or any other variant.
Date
object and its methods to handle dates.
Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform.
var answer = 42And later, you could assign the same variable a string value, for example,
answer = "Thanks for all the fish..."Because JavaScript is loosely typed, this assignment does not cause an error message. In expressions involving numeric and string values, JavaScript converts the numeric values to strings. For example, consider the following statements:
x = "The answer is " + 42The first statement returns the string "The answer is 42." The second statement returns the string "42 is the answer."
y = 42 + " is the answer."
Number_hits
, temp99
, and _name
.
var
to declare a global variable is optional. However, you must use var
to declare a variable inside a function.
You can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called phoneNumber
is declared in a FRAMESET
document, you can refer to this variable from a child frame as parent.phoneNumber
.
For information on using variables across frames and windows, see Chapter 4, "Using Windows and Frames."
Literals
You use literals to represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script.
Integers
Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base 8). A decimal integer literal consists of a sequence of digits without a leading 0 (zero). A leading 0 (zero) on an integer literal indicates it is in octal; a leading 0x (or 0X) indicates hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7.
Some examples of integer literals are: 42 0xFFF, and -345.
Floating-Point Literals
A floating-point literal can have the following parts:
true
and false
.
"
) or single ('
) quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or both double quotation marks. The following are examples of string literals:
"blah"
'blah'
"1234"
"one line \n another line"
Table 8.1 JavaScript special characters
Character | Meaning |
---|---|
\b | backspace |
\f | form feed |
\n | new line |
\r | carriage return |
\t | tab |
\\ | backslash character |
Escaping Characters
For characters not listed in Table 8.1, a preceding backslash is ignored, with the exception of a quotation mark and the backslash character itself.
You can insert a quotation mark inside a string by preceding it with a backslash. This is known as escaping the quotation mark. For example,
var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service."
The result of this would be
document.write(quote)He read "The Cremation of Sam McGee" by R.W. Service.
To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file path c:\temp
to a string, use the following:
var home =
"
c:\\temp"
Array Literals
As discussed in Chapter 11, "Predefined Core Objects and Functions," JavaScript provides array objects. In earlier versions of Navigator, you could create arrays only using the array constructor function. In Navigator 4.0, however, you can also create arrays using literal notation. The syntax for creating a literal array is:
arrayName = [element0, element1, ..., elementN]
where arrayName
is the name of the new array and each element
I
is a value for on of the array's elements. When you create an array using literal notation, it is initialized with the specified values as its elements, and its length is set to the number of arguments.
You do not have to specify all elements in the new array. If you put 2 commas in a row, the array is created with spaces for the unspecified elements, as shown in the second example below.
If an array is created using literal notation in a top-level script, JavaScript interprets the object each time it evaluates the expression containing the array literal. In addition, a literal used in a function is created each time the function is called.
The following example creates the coffees
array with three elements and a length of three:
coffees = ["French Roast", "Columbian", "Kona"]
The following example creates the fish
array, giving values to two elements and having one empty element:
fish = ["Lion", , "Surgeon"]
With this expression fish[0]
is "Lion", fish[2]
is "Surgeon", and fish[1]
is undefined.
Object Literals
In earlier versions of Navigator, you could create objects only using their constructor functions or using a function supplied by some other object for that purpose. In Navigator 4.0, however, you can also create objects using literal notation. The syntax for creating a literal object is:
objectName = {property1:value1, property2:value2,..., propertyN:valueN}
where objectName
is the name of the new object, each property
I
is an identifier (either a name, a number, or a string literal), and each value
I
is an expression whose value is assigned to the property
I
.
If an object is created using literal notation in a top-level script, JavaScript interprets the object each time it evaluates the expression containing the object literal. In addition, a literal used in a function is created each time the function is called.
Assume you have the following statement:
if (cond) x = {hi:"there"}
In this case, JavaScript makes the literal object and assigns it to the variable x
if and only if the expression cond
is true.
The following example creates myHonda
with three properties. Note that the engine
property is also an object with its own properties.
myHonda = {color:"red",wheels:4,engine:{cylinders:4,size:2.2}}
Regular Expression Literals
Navigator 4.0 introduces regular expressions for doing pattern matching in strings. Regular expressions can be constructed using a literal notation. For more information, see "Regular Expressions".
Last Updated: 10/31/97 10:37:34