A string literal is zero or more characters enclosed in matching single or double quotes. Each character may be represented by an escape sequence.
You can escape special characters with special escape sequences. You can also escape any character and specify it by its octal, hexadecimal or Unicode equivalent code point. Note that the octal values will be in the range 0 to 377 and the hexadecimal values will be in the range 0 to FF. The octal and hexadecimal escapes can only cover the first 256 character codes, some of which are control codes and should not be used anyway. The Unicode escape gives access to the full 65536 character codes in the Unicode set. Although you can specify octal or hexadecimal values, there is presently no standardized decimal-based escape mechanism. You'll just have to learn octal or hexadecimal, unfortunately.
Here are the valid common escape sequences:
Escape Sequence | Name | Symbol |
\" | Double Quote | " |
\' | Single Quote (Apostrophe) | ' |
\\ | Backslash | \ |
\a | Audible alert (MSIE displays the letter a) | <BEL> |
\b | Backspace (ignored silently in MSIE) | <BS> |
\f | Form Feed (ignored silently in MSIE) | <FF> |
\n | Line Feed (Newline - MSIE inserts a space) | <LF> |
\r | Carriage Return (MSIE inserts a space) | <CR> |
\t | Horizontal Tab (MSIE inserts a space) | <HT> |
\v | Vertical tab (MSIE displays the letter v) | <VT> |
\0nn | Octal escape | - |
\042 | Double Quote | " |
\047 | Single Quote (Apostrophe) | ' |
\134 | Backslash | \ |
\xnn | Hexadecimal escape | - |
\x22 | Double Quote | " |
\x27 | Single Quote (Apostrophe) | ' |
\x5C | Backslash | \ |
\unnnn | Unicode escape | - |
\u0022 | Double Quote | " |
\u0027 | Single Quote (Apostrophe) | ' |
\u005C | Backslash | \ |
\uFFFE | A special Unicode sentinel character for flagging byte reversed text | - |
\uFFFF | A special Unicode sentinel character | - |
Here are some example string literals:
myString = "James Bond";
myString = 'Another String';
myString = 'A string with double " quotes';
myString = "He's got a single quote";
The characters in the quotes are converted to a String primitive value and will replace the expression in the context in which it has been used. This would normally be an assignment of a variable or perhaps part of a relational expression.
There are circumstances in HTML documents where JavaScript string delimiters may need to be single quotes, because the fragment of JavaScript is already enclosed in double quotes that are part of the HTML source code space.
For example:
<A HREF="javascript:passStringValue('ABCDEF');">ABCDEF</A>;
You can use double quotes without breaking the syntax rules of the HTML page containing the JavaScript.
You can exchange the pairs of quotes around between the contexts but it is good to stick one or the other. It tends to work out that double quotes are used in HTML which forces single quotes to be used in the JavaScript fragments that are placed into HTML tag attributes.
This is discussed with examples in the "Escaped JavaScript quotes in HTML" topic .
If you use quotes in JavaScript that you plan to use inside HTML, then be sensible about the use of single and double quotes. Often you will find that double quotes will break, even though they are enclosed in single quotes when you include them in an HREF for an anchor. For example, this can break because the double quotes are seen by the HTML parser:
<A HREF="javascript:handleClick('bad " quotes');">
Put an escape round the quotes. Sometimes a backslash is appropriate and sometimes an HTML character entity depending on who you are trying to hide the quotes from.
Be careful if you use HTML escape sequences such as ' in JavaScript string literals. Some implementations will unescape that string in the JavaScript context and not the HTML context hoped for. That will restore the single quote inside the JavaScript string literal causing a run-time error. You should escape single quotes with a backslash.
You cannot include a line terminator within a string literal. Instead use a newline (\n) escape sequence.
Currency symbols are notoriously non-portable. Check that your target audience can display international currency symbols before using GB pounds or Euro symbols. Although these are defined in the standards, they are often missing from the installed character sets.
ECMA 262 edition 2 - section 7.7.3
ECMA 262 edition 2 - section 7.7.4
ECMA 262 edition 3 - section 7.8.4
O'Reilly JavaScript Definitive Guide - page 38
Prev | Home | Next |
String concatenate (+) | Up | String object |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |