Table of Contents | Previous
| Next
| Index
This chapter discusses values that JavaScript recognizes and describes the fundamental building blocks of JavaScript expressions: variables and literals.
This chapter contains the following sections:
JavaScript recognizes the following types of values:
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 JavaScript. However, you can use the 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.
JavaScript is a dynamically typed language. That means you do not have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution. So, for example, you could define a variable as follows:
var answer = 42
And later, you could assign the same variable a string value, for example,
answer = "Thanks for all the fish..."
Because JavaScript is dynamically typed, this assignment does not cause an error message.
In expressions involving numeric and string values with the + operator, JavaScript converts numeric values to strings. For example, consider the following statements:
x = "The answer is " + 42 // returns "The answer is 42"
y = 42 + " is the answer" // returns "42 is the answer"
In statements involving other operators, JavaScript does not convert numeric values to strings. For example:
"37" - 7 // returns 30
"37" + 7 // returns 377
You use variables as symbolic names for values in your application. You give variables names by which you refer to them and which must conform to certain rules.
A JavaScript identifier, or name, must start with a letter or underscore ("_"); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
Some examples of legal names are Number_hits
, temp99
, and _name
.
You can declare a variable in two ways:
A variable or array element that has not been assigned a value has the value undefined
. The result of evaluating an unassigned variable depends on how it was declared:
The following code demonstrates evaluating unassigned variables.
function f1() {
return y - 2;
}
f1() //Causes runtime error
function f2() {
return var y - 2;
}
f2() //returns NaN
You can use undefined
to determine whether a variable has a value. In the following code, the variable input
is not assigned a value, and the if
statement evaluates to true
.
var input;
if(input === undefined){
doThis();
} else {
doThat();
}
The undefined
value behaves as false when used as a Boolean value. For example, the following code executes the function myFunction
because the array element is not defined:
myArray=new Array()
if (!myArray[0])
myFunction()
When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in Boolean contexts. For example:
var n = null
n * 32 //returns 0
When you set a variable identifier by assignment outside of a function, it is called a global variable, because it is available everywhere in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within the function.
Using 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
.
You use literals to represent values in JavaScript. These are fixed values, not variables, that you literally provide in your script. This section describes the following types of literals:
An array literal is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ([]). When you create an array using an array literal, it is initialized with the specified values as its elements, and its length is set to the number of arguments specified.
The following example creates the coffees
array with three elements and a length of three:
coffees = ["French Roast", "Columbian", "Kona"]
NOTE:
An array literal is a type of object initializer. See "Using Object Initializers" on
page 101.
If an array is created using a literal in a top-level script, JavaScript interprets the array 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.
Array literals are also Array
objects. See "Array Object" on page 107 for details on Array
objects.
You do not have to specify all elements in an array literal. If you put two commas in a row, the array is created with spaces for the unspecified elements. The following example creates the fish
array:
fish = ["Lion", , "Angel"]
This array has two elements with values and one empty element (fish[0]
is "Lion", fish[1]
is undefined, and fish[2]
is "Angel"):
If you include a trailing comma at the end of the list of elements, the comma is ignored. In the following example, the length of the array is three. There is no myList[3]
. All other commas in the list indicate a new element.
myList = ['home', , 'school', ];
In the following example, the length of the array is four, and myList[0]
is missing.
myList = [ , 'home', , 'school'];
In the following example, the length of the array is four, and myList[3]
is missing. Only the last comma is ignored. This trailing comma is optional.
myList = ['home', , 'school', , ];
The Boolean type has two literal values: true
and false
.
Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object. The Boolean object is a wrapper around the primitive Boolean data type. See "Boolean Object" on page 111 for more information.
A floating-point literal can have the following parts:
The exponent part is an "e" or "E" followed by an integer, which can be signed (preceded by "+" or "-"). A floating-point literal must have at least one digit and either a decimal point or "e" (or "E").
Some examples of floating-point literals are 3.1415, -3.1E12, .1e12, and 2E-12
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.
An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). You should not use an object literal at the beginning of a statement. This will lead to an error.
The following is an example of an object literal. The first element of the car
object defines a property, myCar
; the second element, the getCar
property, invokes a function (Cars("honda")
); the third element, the special
property, uses an existing variable (Sales
).
var Sales = "Toyota";
function CarTypes(name) {
if(name == "Honda")
return name;
else
return "Sorry, we don't sell " + name + ".";
}
car = {myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales}
document.write(car.myCar); // Saturn
document.write(car.getCar); // Honda
document.write(car.special); // Toyota
Additionally, you can use an index for the object, the index
property (for example, 7), or nest an object inside another. The following example uses these options. These features, however, may not be supported by other ECMA-compliant browsers.
car = {manyCars: {a: "Saab", b: "Jeep"}, 7: "Mazda"}
document.write(car.manyCars.b); // Jeep
document.write(car[7]); // Mazda
A string literal is zero or more characters enclosed in double ("
) 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:
You can call any of the methods of the String object on a string literal value--JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length
property with a string literal.
You should use string literals unless you specifically need to use a String object. See "String Object" on page 118 for details on String
objects.
In addition to ordinary characters, you can also include special characters in strings, as shown in the following example.
"one line \n another line"
The following table lists the special characters that you can use in JavaScript strings.
Table 2.1 JavaScript special characters
For characters not listed in Table 2.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."
document.write(quote)
The result of this would be
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"
Unicode is a universal character-coding standard for the interchange and display of principal written languages. It covers the languages of Americas, Europe, Middle East, Africa, India, Asia, and Pacifica, as well as historic scripts and technical symbols. Unicode allows for the exchange, processing, and display of multilingual texts, as well as the use of common technical and mathematical symbols. It hopes to resolve internationalization problems of multilingual computing, such as different national character standards. Not all modern or archaic scripts, however, are currently supported.
The Unicode character set can be used for all known encoding. Unicode is modeled after the ASCII (American Standard Code for Information Interchange) character set. It uses a numerical value and name for each character. The character encoding specifies the identity of the character and its numeric value (code position), as well as the representation of this value in bits. The 16-bit numeric value (code value) is defined by a hexadecimal number and a prefix U, for example, U+0041 represents A. The unique name for this value is LATIN CAPITAL LETTER A.
JavaScript versions prior to 1.3.
Unicode is not supported in versions of JavaScript prior to 1.3.
Unicode is compatible with ASCII characters and is supported by many programs. The first 128 Unicode characters correspond to the ASCII characters and have the same byte value. The Unicode characters U+0020 through U+007E are equivalent to the ASCII characters 0x20 through 0x7E. Unlike ASCII, which supports the Latin alphabet and uses 7-bit character set, Unicode uses a 16-bit value for each character. It allows for tens of thousands of characters. Unicode version 2.0 contains 38,885 characters. It also supports an extension mechanism, Transformation Format (UTF), named UTF-16, that allows for the encoding of one million more characters by using 16-bit character pairs. UTF turns the encoding to actual bits.
Unicode is fully compatible with the International Standard ISO/IEC 10646-1; 1993, which is a subset of ISO 10646, and supports the ISO UCS-2 (Universal Character Set) that uses two-octets (two bytes or 16 bits).
JavaScript and Navigator support for Unicode means you can use non-Latin, international, and localized characters, plus special technical symbols in JavaScript programs. Unicode provides a standard way to encode multilingual text. Since Unicode is compatible with ASCII, programs can use ASCII characters. You can use non-ASCII Unicode characters in the comments and string literals of JavaScript.
You can use the Unicode escape sequence in string literals. The escape sequence consists of six ASCII characters: \u and a four-digit hexadecimal number. For example, \u00A9 represents the copyright symbol. Every Unicode escape sequence in JavaScript is interpreted as one character.
The following code returns the copyright symbol and the string "Netscape Communications".
x="\u00A9 Netscape Communications"
The following table lists frequently used special characters and their Unicode value.
Table 2.2 Unicode values for special characters
The JavaScript use of the Unicode escape sequence is different from Java. In JavaScript, the escape sequence is never interpreted as a special character first. For example, a line terminator escape sequence inside a string does not terminate the string before it is interpreted by the function. JavaScript ignores any escape sequence if it is used in comments. In Java, if an escape sequence is used in a single comment line, it is interpreted as an Unicode character. For a string literal, the Java compiler interprets the escape sequences first. For example, if a line terminator escape character (\u000A) is used in Java, it terminates the string literal. In Java, this leads to an error, because line terminators are not allowed in string literals. You must use \n for a line feed in a string literal. In JavaScript, the escape sequence works the same way as \n.
You can use Unicode to display the characters in different languages or technical symbols. For characters to be displayed properly, a client such as Netscape Navigator 4.x needs to support Unicode. Moreover, an appropriate Unicode font must be available to the client, and the client platform must support Unicode. Often, Unicode fonts do not display all the Unicode characters. Some platforms, such as Windows 95, provide a partial support for Unicode.
To receive non-ASCII character input, the client needs to send the input as Unicode. Using a standard enhanced keyboard, the client cannot easily input the additional characters supported by Unicode. Often, the only way to input Unicode characters is by using Unicode escape sequences. The Unicode specification, however, does not require the use of escape sequences. Unicode delineates a method for rendering special Unicode characters using a composite character. It specifies the order of characters that can be used to create a composite character, where the base character comes first, followed by one or more non-spacing marks. Common implementations of Unicode, including the JavaScript implementation, however, do not support this option. JavaScript does not attempt the representation of the Unicode combining sequences. In other words, an input of a
and '
does not produce à
. JavaScript interprets a'
as two distinct 16-bit Unicode characters. You must use a Unicode escape sequence or a literal Unicode character for à
.
For more information on Unicode, see the Unicode Consortium Web site and The Unicode Standard, Version 2.0, published by Addison-Wesley, 1996.
Table of Contents | Previous
| Next
| Index
Last Updated: 05/27/99 21:21:20
Copyright (c) 1999
Netscape Communications Corporation