Not everything that starts with a backslash is an escape sequence. Many characters are just not useful to escape sequences, and will simply cause a preceding backslash to be ignored.

"\\H\\e\\l\\l\\o" === "Hello" // true

On the other hand, some characters like “u” and “x” will cause a syntax error when used improperly after a backslash. The following is not a valid string literal because it contains the Unicode escape sequence prefix \\u followed by a character that is not a valid hexadecimal digit nor a curly brace:

"C:\\Windows\\System32\\updatehandlers.dll" // SyntaxError

A backslash at the end of a line inside a string does not introduce an escape sequence, but indicates line continuation, i.e.

"contin\\
uation" === "continuation" // true

Similarity to other formats

While escape sequences in JavaScript bear resemblance to other languages and formats, like C++, Java, JSON, etc. there will often be critical differences in the details. When in doubt, be sure to test that your code behaves as expected, and consider checking the language specification.

Entering special characters in strings and regular expressions

Most printable characters can be included in string or regular expression literals just as they are, e.g.

var str = "ポケモン"; // a valid string
var regExp = /[Α-Ωα-ω]/; // matches any Greek letter without diacritics

In order to add arbitrary characters to a string or regular expression, including non-printable ones, one has to use escape sequences. Escape sequences consist of a backslash (“\”) followed by one or more other characters. To write an escape sequence for a particular character, one typically (but not always) needs to know its hexadecimal character code.

JavaScript provides a number of different ways to specify escape sequences, as documented in the examples in this topic. For instance, the following escape sequences all denote the same character: the line feed (Unix newline character), with character code U+000A.

Escape sequence types