A literal in JavaScript is a data value that appears directly in a program. These are numbers, strings (in single or double quotes), the boolean values true and false, and the special value null. The specific syntax of each type of literal is described in the following subsections.
Base-10 integers may be represented simply as an optional minus sign followed by a sequence of digits that does not begin with the digit zero.
[-](1-9)(0-9)*
For example:
3 -12 10000000
Since JavaScript represents all numbers as floating-point values, you can specify extremely large integer values, but you may lose precision in the trailing digits.
You may also specify integers as octal (base-8) and hexadecimal (base-16) values. An octal value begins with an optional minus sign, followed by the digit zero, followed by a sequence of digits, each between 0 and 7:
[-]0(0-7)*
As in C and C++, a hexadecimal literal begins with an optional minus sign followed by "0x" or "0X", followed by a string of hexadecimal digits. A hexadecimal digit is one of the digits 0 through 9, or the letters a (or A) through f (or F), which are used to represent values ten through fifteen.
[-]0(x|X)(0-9|a-f|A-F)*
Examples:
-0123 0377 0xff -0xCAFE911
Floating-point literals can have a decimal point; they use the traditional syntax for scientific notation exponents. A floating-point value is represented as:
Exponential notation may be represented with additional syntax:
More succinctly, the syntax is:
[(+|-)][digits][.digits][(E|e)[(+|-)]digits]
Examples:
3.14 -1.414 .333333333333333333 6.02e+23 1.4738223E-32
Note that JavaScript does not specify the maximum and minimum representable sizes of numbers. It is probably safe to assume that every implementation uses IEEE double-precision format, which has a maximum value of approximately +/-1.79E+308 and a minimum value of approximately +/-4.94E-324.
Strings are any sequence of zero or more characters enclosed within single or double quotes (' or "). Double-quote characters may be contained within strings delimited by single-quote characters, and single-quote characters may be contained within strings delimited by double quotes. Examples of string literals are:
'testing' "3.14" 'name="myform"' "Wouldn't you prefer O'Reilly's book?"
HTML uses double-quoted strings.[2] Since JavaScript code often contains embedded HTML strings, and is often embedded within HTML strings (for event handler specifications), it is a good idea to use single quotes around your JavaScript strings. In the example below, the string "Thank you" is single-quoted within a JavaScript expression, which is double-quoted within an HTML event-handler attribute:
[2] The original versions of HTML required double-quoted strings, though most popular web browsers now allow single-quoted strings as HTML attribute values as well.
<A HREF="index.html" onClick="alert('Thank you')">Click Me</A>
On the other hand, when you use single quotes to delimit your strings, you must be careful with English contractions and possessives like "can't" and "O'Reilly's". Since the apostrophe is the same as the single-quote character, you must use the backslash character (\) to escape any apostrophes that appear in single-quoted strings. This use of the backslash is explained in the section that follows.
The backslash character (\) has a special purpose in JavaScript strings. Combined with the character that follows it, it represents a character that is not otherwise representable within the string, just like in C or C++. For example, the characters \n are an escape sequence that represents a newline character. When we type the string literal, we type two individual characters, the backslash and the n, but the string itself contains only a single newline character at that location.[3]
[3] Bear in mind that HTML ignores newlines, so a \n escape sequence in HTML will not produce a newline in the browser display: for that you need to output <BR> or <P>. Thus, the \n escape might be useful in a string you pass to alert(), but not in a string you pass to document.write().
Another example, mentioned above, is the \' escape which represents the single quote (or apostrophe) character. This escape sequence is useful when you need to include an apostrophe in a string literal which is contained within single quotes. You can see why we call these "escape sequences"--the backslash allows us to "escape" from the usual interpretation of the single-quote character; instead of using it to mark the end of the string, we use it as an apostrophe. Table 2.1 lists the JavaScript escape sequences and the characters they represent.
There is one escape sequence that deserves special comment. \xxx represents the character with the Latin-1 (ISO8859-1) encoding specified by the three octal digits xxx. You can use this escape sequence to embed accented characters and special symbols into your JavaScript code, even though those characters cannot be typed from a standard keyboard. For example, the sequence \251 represents the copyright symbol.
Sequence | Character Represented |
---|---|
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Tab |
\' | Apostrophe or single quote |
\" | Double quote |
\xxx |
The character with the encoding specified by the three octal digits xxx. |
The boolean data type in JavaScript represents a "truth value"--i.e., whether something is true or false. Any kind of comparison operation in JavaScript yields a boolean value that specifies whether the comparison succeeded or failed. Since there are two possible truth values, there are two boolean literals: the keywords true or false. These literals are commonly used in JavaScript code like the following:
while(done !=index.html true) { ... if ((a == true) || (b == false) || (i > 10)) done = true; }
There is one final literal used in JavaScript: the null keyword. All other literals represent a value of a particular data type. null is different--it represents a lack of value. In a sense, null is like zero, but for data types other than numbers. We'll see more about null in Chapter 3, Variables and Data Types.
file: /Techref/language/java/script/definitive/ch02_05.htm, 14KB, , updated: 2019/10/14 15:00, local time: 2024/11/14 23:20,
18.219.186.229:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://linistepper.com/techref/language/java/script/definitive/ch02_05.htm"> [Chapter 2] 2.5 Literals</A> |
Did you find what you needed? |