JavaScript Special Characters
In JavaScript you can add special characters to a text string by using the backslash sign.
Insert Special Characters
The backslash (\) is used to insert apostrophes, new lines, quotes, and other special characters into a text string.
Look at the following JavaScript code:
var txt="We are the so-called "Vikings" from the north.";
document.write(txt); |
In JavaScript, a string is started and stopped with either single or
double quotes. This means that the string above will be chopped to: We
are the so-called
To solve this problem, you must place a backslash (\) before each
double quote in "Viking". This turns each double quote into a string
literal:
var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt); |
JavaScript will now output the proper text string: We are the so-called "Vikings" from the north.
Here is another example:
document.write ("You \& I are singing!"); |
The example above will produce the following output:
The table below lists other special characters that can be added to a text string with the backslash sign:
Code |
Outputs |
\' |
single quote |
\" |
double quote |
\& |
ampersand |
\\ |
backslash |
\n |
new line |
\r |
carriage return |
\t |
tab |
\b |
backspace |
\f |
form feed |
Create a free Flash website with our simple, online web design editing platform. Stunning templates
and user-friendly tools make website building easy and fun.
Start Creating your free website now!

Whether you're new to XML or already an advanced user, the
user-friendly views and powerful entry helpers, wizards, and debuggers
in XMLSpy are designed to meet your XML and Web development needs from
start to finish.
New features in Version 2011!
- XML editor
- Graphical XML Schema / DTD editors
- XSLT 1.0/2.0 editor, debugger, profiler
- XQuery editor, debugger, profiler
- XBRL validator, taxonomy editor, taxonomy wizard
- New! Chart creation for XML data
- Support for Office Open XML (OOXML)
- Graphical WSDL 1.1/2.0 editor & SOAP debugger
- JSON editing & conversion
- Java, C#, C++ code generation
- 32-bit and 64-bit versions
- And much more!
Download a free trial today!
|
|
|
 |
W3Schools' Online Certification
The perfect solution for professionals who need to balance work, family, and career building.
More than 6000 certificates already issued!
Get Your Certificate »
|
The HTML Certificate documents your knowledge of HTML.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
|