Special Characters

Because strings must be written within quotes, JavaScript will misunderstand this string:
   var y = "We are the so-called "Vikings" from the north."

Javascript will simply chop this string down to:
   "We are the so-called "

The solution to avoid this problem, is to use the \ escape character. The backslash escape character turns special characters into string characters:

HTML file: Displayed by browser:
<body>
<p id="demo"></p>
<script>
   var x = 'It\'s alright';
   var y = "We are the so-called \"Vikings\" from the north.";
   document.getElementById("demo").innerHTML =
   x + "<br />" + y; ;
</script>
</body>

The backslash escape character (\) can also be used to insert other special characters in a string. This is the list of special characters that can be added to a text string with the backslash sign:

Code Outputs
\' single quote
\" double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed

Back Table of Contents Next