Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Including Special Characters in a JavaScript String

In JavaScript you define a string by placing its characters within double quotes ("). So what if you want to use double quote characters within the string? There are several characters that can cause problems when included in a string. These include double quotes and back slashes (\). Those, and are certain control characters can be included in a string if they are escaped.

An escaped character is preceded with a back slash, for example /" is the escaped character code for a double quote. Shown below is the assignment of a string variable that contains double quotes.

var strCat = "\"If you don't know where you're going, any road will get you there\", said Cheshire cat."

CharacterDescription
\"double quote
\'single quote
\\back slash
\nnew line
\rcarraiage return
\ttab

You can define a string by placing its characters within single quotes instead of double quotes, then you are permitted to include double quotes within the string, but then any single quotes within the string will need to be escaped.

<script>
   alert("This string contains a \n new line character.");
</script>

When the string is the output of a JavaScript element such as an alert box, use \n to create a new line. When the string output is to an HTML page, use the line break tag <br />.

Since an html page is required to flow, it does not have tab stops, so the tab character \t will not work. If you need spaces, use one or more &nbsp; (non-breaking space) character codes, but they you will not be able to hold column positions. You might be trying to do something that requires a table.

More Java Script Code:
• JavaScript to Set Checkbox to Readonly
• Synchronous and Asynchronous Script Loading
• JavaScript Code to Restrict Keyboard Entries
• Easy HTML5 Drag and Drop
• Java Script to Dynamically Add, Remove, and Change Items in a Select List
• Code for Java Script Cylinder / Cone Volume Calculator
• How to Use HTML5 canvas arc and arcTo Functions
• Code to Block the Ad Blockers
• Introduction to JavaScript Programming
• Working With the Keyboard in Java Script

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2018 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268