Escape sequence (\) (Definition)

A means of defining characters that cannot easily be typed on a keyboard.

Availability:

ECMAScript edition - 2
Opera - 3.0

The string delimiter characters present problems if you need to include them inside a string. Typically you may want to include a single quote as an apostrophe, as in the contraction of do not to don't. Or you may want to enclose a spoken comment inside double quotation marks.

Because the single and double quotes are generally interchangeable in JavaScript, in most cases the problem is easy to solve.

To use " inside a string, you can do it like this:

myString = 'A man said "hello" to me';

Using a double quote to include single quotes in your string, you might do this:

myString = "Don't do that";

Using the backslash escape delimiter we can safely use any combination of quotes inside either sort. Like this:

myString = "A man said \"hello\" to me";

myString = 'Don\'t do that';

A further use for this is to escape a single dot character in regular expressions. This allows you to search for full stops in the source text because a dot in a Regular Expression is a wildcard character match. This technique is useful for other meta symbols in the regular expression set. Refer to the regular expression topic for more details.

Just to illustrate here is a simple example. This regular expression matches any single character at the start of a line:

/^./

This variant matches only full stops or periods at the start of the line:

/^\./

An escape sequence is a series of characters that taken together describe a character that cannot normally be represented in the code set that the source is written in or may be difficult to type on keyboards that are not specially designed for international character sets.

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 examples of escape sequences for the apostrophe character.

In HTML apostrophe characters are escaped like this:

&#039;

In C language characters are escaped like this for octal values:

\047

And like this for hexadecimal values:

\x27

And in JavaScript, Unicode characters can be escaped like this:

\u0027

Where the \u is followed by a four digit hexadecimal value.

See also:Cast operator, Character constant, Character display semantics, Character set, Comment, Constant, Constant expression, Lexical convention, Newline, Script Source Text, String literal

Cross-references:

ECMA 262 edition 2 - section - 2

ECMA 262 edition 2 - section - 6

ECMA 262 edition 2 - section - 7.7.3

ECMA 262 edition 3 - section - 2

ECMA 262 edition 3 - section - 6

ECMA 262 edition 3 - section - 7.7

O'Reilly JavaScript Definitive Guide - page - 38