↑
Main Page
Conversions
Figure 2-2
String literals are specified by using either double quotes (“) or single quotes (‘). This differs from Java,
where double quotes are used to specify strings and single quotes are used to specify characters. However,
because ECMAScript has no character type, it is permissible to use either notation. For example, the follow-
ing two lines are valid:
var sColor1 = “blue”;
var sColor2 = ‘blue’;
The string type also encompasses several character literals, which should be very familiar to Java, C, and
Perl developers. The following table lists the ECMAScript character literals:
Literal
Meaning
\n
Newline
\t
Tab
\b
Backspace
\r
Carriage return
\f
Formfeed
\\
Backslash
\’
Single quote
\”
Double quote
\0
nnn
A character represented by octal code
nnn
(where
n
is an
octal digit 0-7)
\x
nn
A character represented by hexadecimal code
nn
(where
n
is a hexadecimal digit 0-F)
\unnnn
A Unicode character represented by hexadecimal code
nnnn (where n is a hexadecimal digit 0-F)
Conversions
One of the most important features of any programming language is the capability to convert between
types, and ECMAScript provides developers with a number of easy conversion routines. Most types
contain methods that provide for simple conversion, and several global methods are available for more
complex conversion. In either case, type conversion is a short, one-step process in ECMAScript.
he l l o !
012345
Position
The string "hello!" has a length of 6.
21
ECMAScript Basics
05_579088 ch02.qxd 3/28/05 11:35 AM Page 21
Free JavaScript Editor
Ajax Editor
©
→