Escaping Characters in JavaScript

Wed, Jul 1, 2009

Tech Tips

Some characters can not be represented in a string in JavaScript, other characters might have a specific meaning in JavaScript.

Here is a list of common escape sequences:

\'   	Single Quote
\n  	Newline
\"  	Double Quote
\t  	Tab
\\  	Backslash
\b  	Backspace
\f  	Form feed
\r  	Carriage return

To use, simply insert them into your string:

var testStr = 'This a \'multi-line\' message.\nHere is the second line.';
alert(testStr);

These are the same escape sequences in the c, c++, and c# worlds.

Bookmark and Share

Comments are closed.