JavaScript Character Escape Sequences
By Stephen Bucaro
Certain characters have programming meaning to the Java Script interpreter, so you
may have problems with them when using them in a string. For example a string is
enclosed in either single or double quotes. You can't use the same type of quote within
the string because when the Java Script interpreter comes upon it, it will think it has
reached the end of the string. Another example is if you want to put a new line in a
string, you just can't hit the keyboard [Enter] key.
If you need to use one of these characters in a string, you need to substitute it
with an "escape sequence". Sometimes escape sequence is called "escape characters".
The sequence of characters in an escape sequence informs the Java Script interpreter
of how to interpret the characters. Shown below are the most useful escape sequences.
Character | Escape Sequence
| ' | \' |
" | \" |
\ | \\ |
new line | \n |
tab | \t |
Shown below is an example of using escape sequences.
This will not work:
var myString = "The file you seek is in the "toys\games" folder";
The same string with characters substituted with escape sequences:
var myString = "The file you seek is in the \"toys\\games\" folder";
More Java Script Code: • Remove Blank Spaces From Ends of a String • The Location Object • Using the Java Script eval() Function • The if/else Structure • Java Script Trigonometric Methods • Comparison Operators • Java Script Math.sin Method • Search and Replace Strings • The Conditional Operator • How to Use a JavaScript try-catch Block to Debug
|