Similar to C/C++, JavaScript uses escape sequences to allow you to place special characters into strings. The escape sequence starts with a backslash (\), followed by another character.
Since the backslash is the escape character, to insert the backslash itself you need to double it (\\).
Escape Sequence |
Character |
\' |
Single quotation mark |
\" |
Double quotation mark |
\\ |
Backslash |
\b |
Backspace |
\f |
Form feed |
\n |
New line |
\r |
Carriage return |
\t |
Horizontal tab |
\ddd |
Octal sequence (3 digits: ddd) |
\xdd |
Hexadecimal sequence (2 digits: dd) |
\udddd |
Unicode sequence (4 hex digits: dddd) |
Example 1:
alert ("Please bring your spouse\\partner to the party");
|
To run the code, paste it into JavaScript Editor, and click the Execute button.
Note that only a single \ is shown.
Example 2:
document.write ("He took a glance and said, "This code produces an error" and indeed it does");
|
To run the code, paste it into JavaScript Editor, and click the Execute button.
What happens? JavaScript Editor highlights the error and explains it, allowing you to make a speedy correction. In this case, the quatation needs to be preceded by an escape character. The corrected version is below:
document.write ("He took a glance and said, \"This code doesn\'t produce an error\" and indeed it doesn\'t");
|
Some useful Unicode symbols:
\u00B1 : superscript 1 : 1
\u00B2 : superscript 2 : 2
The complete list of Unicode characters can be seen at: http://www.unicode.org/charts/
Next |