You can use a backslash in JavaScript to add special characters to a text string.
Insert special characters
A backslash is used to insert ellipsis, line breaks, quotation marks, and other special characters into a text string.
See the following JavaScript code:
var txt="We are the so-called "Vikings" from the north."document.write(txt)
In JavaScript, strings start or end with single or double quotation marks. This means that the above string will be truncated to: We are the so-called.
To solve this problem, you must add a backslash (\) before the quotation marks in "Viking (\). In this way, each double quotation mark can be converted to a literal string.
var txt="We are the so-called \"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:
document.write ("You \& me are singing!")
The preceding example generates the following output:
You & me are singing!
The following table lists other special characters that can be added to a text string using a backslash:
| Code |
Output |
| \' |
Single quotes |
| \" |
Double quotation marks |
| \& |
And number |
| \\ |
Backslash |
| \ N |
Line Break |
| \ R |
Carriage Return |
| \ T |
Tab |
| \ B |
Escape Character |
| \ F |
Page feed |