You can use backslashes in JavaScript to add special characters to a text string.
inserting special characters
The backslash is used to insert ellipses, line breaks, quotes, and other special characters in a text string.
Take a look at the following JavaScript code:
"Vikings"
From the north. "
document.write (TXT)
In JavaScript, strings start or end with either single or double quotes. This means that the above string will be truncated to: We are the so-called.
To solve this problem, you must precede the quotation mark in "Viking" with a backslash (\). This converts each double quote to a literal string.
\"Vikings\"
From the north. "
document.write (TXT)
Now JavaScript can output the correct text string: We are the so-called "Vikings" from the north.
This is another example:
\&
Me are singing! ")
The above example produces the following output:
You & Me are singing!
The following table lists the remaining special characters that can be added to a text string using a backslash:
Code |
Output |
\' |
Single quotation mark |
\" |
Double quotes |
\& |
and number |
\\ |
Back slash |
\ n |
Line feed |
\ r |
return character |
\ t |
Tabs |
\b |
Backspace |
\f |
Page breaks |