JavaScript Special Characters


In JavaScript you can add special characters to a text string by using the backslash sign. The backslash (\) is used to insert apostrophes, new lines, quotes, and other special characters into a text string.

The JavaScript on the right has an error because there are double quotes in a pair of double quotes. To solve this problem, you must place a backslash (\) before each double quote in “North.” This turns each double quote into a string literal.



   

The table below lists other special characters that can be added to a text string with the backslash sign:

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


Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.

     





      What did the fish say when he swam into a wall?    
      “Dam(n).”    
      — Rory Scovel