Escape Characters

In the interactive story example, you saw a JavaScript escape character in action—the \ ", which in a string represents a double quote. Escape characters are used in strings to do the following:

  • Prevent certain characters from causing errors

  • Add formatting (such as carriage returns) to strings

Table 5.1 lists the available escape characters.

Table 5.1. Escape Characters
Sequence Name
\b Backspace
\t Horizontal tab
\f Form feed
\n New line (line feed)
\r Carriage return
\" Double quote
\' Single quote
\\ Backslash

These can be placed anywhere in the string and need no whitespace before or after them.

Here are some examples of escape characters in action:

  • Carriage return

     <script language="JavaScript"> <!-- Cloaking device on! alert("A ...

Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.