Export (0) Print
Expand All

Special Characters (JavaScript)

JavaScript provides escape sequences that you can include in strings to create characters that you cannot type directly.

A string value is a series of zero or more Unicode characters (letters, digits, and other characters). String literals are enclosed in matching pairs of single or double quotation marks. Double quotation marks can be contained in a string that is enclosed in single quotation marks. Single quotation marks can be contained in a string that is enclosed in double quotation marks.

Each character in a string literal can be represented by an escape sequence. An escape sequence starts with a backslash (\) that informs the JavaScript interpreter that the next character is a special character.

You can specify a Unicode character by using the \uhhhh escape sequence, where hhhh is a four-digit hexadecimal number. A Unicode escape sequence can represent any 16-bit character.

You can use a single-character escape sequence for some characters. For example, \t specifies a tab character.

The following table lists escape sequences for several characters.

Unicode character value

Escape sequence

Meaning

Category

\u0008

\b

Backspace

\u0009

\t

Tab

White space

\u000A

\n

Line feed (new line)

Line terminator

\u000B

\v

(See note after this table.)

Vertical tab

White space

\u000C

\f

Form feed

White space

\u000D

\r

Carriage return

Line terminator

\u0020

Space

White space

\u0022

\"

Double quotation mark (")

\u0027

\'

Single quotation mark (')

\u005C

\\

Backslash (\)

\u00A0

Nonbreaking space

White space

\u2028

Line separator

Line terminator

\u2029

Paragraph separator

Line terminator

\uFEFF

Byte order mark

White space

The Category column specifies whether the character is a white space or line terminator character. The trim Method (String) (JavaScript) removes leading and trailing white space and line terminator characters from a string.

The backslash itself is used as the escape character. Therefore, you cannot directly type one in your script. If you want to write a backslash, you must type two of them together (\\).

Note Note

Starting in Internet Explorer 9 standards mode, Internet Explorer 10 standards mode, Internet Explorer 11 standards mode, and Windows Store apps, you cannot identify the browser as Internet Explorer by testing for the equivalence of the vertical tab (\v) and the "v". In earlier versions, the expression "\v" === "v" returns true. In Internet Explorer 9 standards mode, Internet Explorer 10 standards mode, Internet Explorer 11 standards mode, and Windows Store apps, the expression returns false.

The following example demonstrates the \\ and \' escape sequences.

document.write('The image path is C:\\webstuff\\mypage\\gifs\\garden.gif.');
document.write ("<br />");
document.write('The caption reads, "After the snow of \'97. Grandma\'s house is covered."');
Show:
© 2014 Microsoft