Java Reference
In-Depth Information
tex t data
Another term for one or more characters of text is a string . You tell JavaScript that text is to be
treated as text and not as code simply by enclosing it inside quotation marks ( " ). For example,
"Hello World" and "A" are examples of strings that JavaScript will recognize. You can also use the
single quotation marks ( ' ), so 'Hello World' and 'A' are also examples of strings that JavaScript
will recognize. However, you must end the string with the same quotation mark that you started it
with. Therefore, "A' is not a valid JavaScript string, and neither is 'Hello World" .
What if you want a string with a single quotation mark in the middle, say a string like Peter
O'Toole ? If you enclose it in double quotes, you'll be fine, so "Peter O'Toole" is recognized by
JavaScript. However, 'Peter O'Toole' will produce an error. This is because JavaScript thinks that
your text string is Peter O (that is, it treats the middle single quote as marking the end of the string)
and falls over wondering what the Toole' is.
Another way around this is to tell JavaScript that the middle ' is part of the text and is not
indicating the end of the string. You do this by using the backslash character ( \ ), which has special
meaning in JavaScript and is referred to as an escape character . The backslash tells the browser that
the next character is not the end of the string, but part of the text. So 'Pete r O\'To ole' will work
as planned.
What if you want to use a double quote inside a string enclosed in double quotes? Well, everything
just said about the single quote still applies. So 'Hello "Paul"' works, but "Hello "Paul"" won't.
However, "Hello \"Paul\"" will work.
JavaScript has a lot of other special characters, which can't be typed in but can be represented using
the escape character in conjunction with other characters to create escape sequences . These work
much the same as in HTML. For example, more than one space in a row is ignored in HTML, so a
space is represented by the term   . Similarly, in JavaScript you'll find instances where you can't
use a character directly but must use an escape sequence. The following table details some of the
more useful escape sequences.
esCape sequenCes
CharaCter represented
Backspace
\b
Form feed
\f
New line
\n
Carriage return
\r
Tab
\t
Single quote
\'
Double quote
\"
Backslash
\\
NN is a hexadecimal number that identifies a character in the
Latinā€1 character set.
\x NN
 
Search WWH ::




Custom Search