String literal (Primitive value)

A literal constant whose type is a built-in primitive value.

Availability:

ECMAScript edition - 2
Property/method value type:String primitive

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 SequenceNameSymbol
\"Double Quote"
\'Single Quote (Apostrophe)'
\\Backslash\
\aAudible alert (MSIE displays the letter a)<BEL>
\bBackspace (ignored silently in MSIE)<BS>
\fForm Feed (ignored silently in MSIE)<FF>
\nLine Feed (Newline - MSIE inserts a space)<LF>
\rCarriage Return (MSIE inserts a space)<CR>
\tHorizontal Tab (MSIE inserts a space)<HT>
\vVertical tab (MSIE displays the letter v)<VT>
\0nnOctal escape-
\042Double Quote"
\047Single Quote (Apostrophe)'
\134Backslash\
\xnnHexadecimal escape-
\x22Double Quote"
\x27Single Quote (Apostrophe)'
\x5CBackslash\
\unnnnUnicode escape-
\u0022Double Quote"
\u0027Single Quote (Apostrophe)'
\u005CBackslash\
\uFFFEA special Unicode sentinel character for flagging byte reversed text-
\uFFFFA 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 .

Warnings:

See also:Escape sequence (\), Escaped JavaScript quotes in HTML, Identifier, Implicit conversion, Line terminator, Literal, String, String concatenate (+), Unicode

Cross-references:

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