JavaScript Literals
Literals are used to represent fixed values in JavaScript. Types of literals include:
Array Literals
An array has zero or elements, each represented by an expression, and is enclosed in square brackets ([]). You specify the values for the elements when you create the array. The length of the array is the number of arguments you specify. For example, the following array, composers, has four elements:
Composers = ["Johann Sebastian Bach", "Ludwig Van Beethoven", "Hector Berlioz", "Beatles"]
An array literal is a type of object initializer. See "Using Object Initializers" on page 101.
Array literals are alsoArray objects. See "Array Object" on page 107 for details on Array objects.
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. Reword this paragraph when I understand it.
Including Empty Elements in Array Literals
To include an empty element in an array literal, put two commas in a row. A trailing comma at the end of the array literal is ignored. For example, if you specify an array called alphabet:
|
alphabet = |
Length |
Contents of alphabet[0] |
Contents of alphabet[1] |
Contents of alphabet[2] |
Contents of alphabet[3] |
["A", ,"C"] |
3 |
A |
empty |
C |
does not exist |
["A", ,"C",] |
3 |
A |
empty |
C |
does not exist |
[,"B", ,"D"] |
4 |
empty |
B |
empty |
D |
["A", ,"C", ,] |
4 |
A |
empty |
C |
empty |
Boolean Literals
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.
Floating-Point Literals
A floating-point literal can have the following parts:
- A decimal integer
- A decimal point (".")
- A fraction (another decimal number)
- An exponent ("e" or "E" followed by an integer, optionally signed (preceded by "+" or "-")
A floating-point literal must have at least one digit and either a decimal point or "e" (or "E"), for example 1.2345, -9.9E11, .3e15, and 5E-10
Integer literals
Integers: |
Base |
Examples |
Contents/format |
decimal |
10 |
-1, 42 |
A sequence of digits in the range 0 to 9 without a leading 0 (zero). |
hexadecimal |
16 |
0xF3F |
A sequence of digits in the range 0 to 9 and/or letters in the range A to F or a to f, beginning with 0x or 0X (leading zero). |
octal |
8 |
07, 0171 |
A sequence of digits in the range 0 to 7 with a leading 0 (zero). |
Object Literals
An object literal consists of a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). Using one at the start of a statement generates an error, however.
In the following example, topic is an object with 3 elements, the first property is defined (mySubject: "JavaScript"), the second invokes a function (getSubject: subjectTypes("HTML")), and the third (mainArea) uses an existing variable (subject).
var subject = "Web";
function subjectTypes(topic) {
if(topic == "HTML")
return "I also know " + topic + ". ";
else
return "I'm afraid I don't know " + topic + " yet. ";
}
topic = {mySubject:"JavaScript", getSubject:subjectTypes("HTML"), mainArea:subject}
document.write("My specialist subject is " + topic.mySubject + ". "); // JavaScript
document.write(topic.getSubject); // HTML
document.write("I know other " + topic.mainArea + " topics. "); // Web
topic = {mySubject: "JavaScript", getSubject: subjectTypes("DHTML"), mainArea: subject}
document.write(topic.getSubject); // DHTML
You can also nest one object inside another, for example:
topic2 = {multiSubjects: {a: "HTML", b: "DHTML", c :"CGI"}, other: "Cooking"}
document.write(topic2.multiSubjects.c); // CGIIn Netscape 4, but not IE4, you can use an index (e.g. 2) for the property as follows:
Topic3 = {mySubject: "JavaScript", 2: subjectTypes("DHTML"), mainArea: subject}
document.write(topic3[2]); // DHTML - index (2) doesn't work in IE4
String Literals
A string literal comprises zero or more characters enclosed in either double (") or single (') quotation marks (not a mixture of the two). For example:
- "Hello Universe"
- 'Hello Universe '
- "42"
- "first line \n second line" (\n is a special character - see below)
If you call any String object method 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.
Using Special Characters in Strings
You can include special characters as listed in the table below in JavaScript strings, for example:
"first line \n second line"
JavaScript special characters:
| Character | Meaning | Character | Meaning |
| \b | Backspace | \t | Tab |
| \f | Form feed | \' | Apostrophe or single quote |
| \n | New line | \" | Double quote |
| \r | Carriage return | \\ | Backslash character (\) |
| \XXX |
Latin-1 encoded character in octal range 0 to 377. E.g. \256 = 'registered' symbol (®). |
||
| \xXX |
Latin-1 encoded character in hexadecimal range 0 to FF. E.g. \xAE = 'registered' symbol. |
||
| \uXXXX |
Unicode encoded character in hexadecimal. E.g. \u00AE = 'registered' symbol. See "Unicode Escape Sequences" on page 44 |
||
Escaping Characters
Backslashes preceding characters other than those in the table above are ignored.
To insert a quotation mark inside a string, precede it with a backslash (also known as escape the quotation mark). E.g.:
var quote = "He suggested \"Vanishing Point\" by Morris West."
document.write(quote)
Results in: He suggested "Vanishing Point" by Morris West.
To include a literal backslash inside a string, escape the backslash character. So, to assign the file path d:\windows to a string, use the following:
var home = "d:\\windows"
Unicode
Unicode is a universal character-coding standard for the interchange and display of principal written languages. A 16-bit numeric value in the form of the prefix U followed by a hexadecimal number defines each letter. For example, U+0041 represents the Latin capital letter A. The Unicode characters U+0020 through U+007E are equivalent to the ASCII characters 0x20 through 0x7E. Unicode is compatible with ASCII. You can use non-ASCII Unicode characters in the comments and string literals (using a preceding \) of JavaScript. JavaScript uses the Unicode escape sequence differently to Java. To display the characters in different languages or technical symbols, the browser and client platform need to support Unicode and the client must support a suitable font. Unicode is not supported in versions of JavaScript prior to 1.3.
For further details follow link to Unicode.
