You can include single quotes in a string by enclosing the string in double quotes, or you can include double quotes in a string by enclosing the string in single quotes. In the situation where you need to use more than two levels of quotes, you can escape quote characters by placing a backslash character in front of a quote character.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Win $3,500.00 worth of gas!

Include a Quote Character in a String

To declare a variable as type String in Java Script requires that you enclose the data within quotes. But what if you want to include quotes within the string? You can easily include single quotes within a string by enclosing the string in double quotes, as shown below.

var aStr = "John said, 'Java Script is fun to use'";

You can include double quotes within a string by enclosing the string in single quotes, as shown below.

var aStr = 'John said, "Java Script is fun to use"';

But what if you want to use Java Script to display the message shown below?

John said, "enclose a sting in quotes like this: "string text""

You can't just alternate single and double quotes as shown below.

aStr = "John said, 'enclose a sting in quotes like this: "string text"'";

Since a double quote marks the beginning of the string, Java Script will assume that next double quote character it encounters marks the end of the sting. Finding a character, other than a ; after the next double quote, will cause Java Script to generate an error.

In the situation where you need to use more than two levels of quotes, you can escape quote characters by placing a backslash character in front of the quote character, as shown below.

aStr = "John said, 'enclose a sting in quotes like this: \"string text\"'";

When Java Script encounters a backslash in a string, it considers the backslash and the character immediately following the backslash to be an escape sequence. An escape sequence provides special instructions to the interpreter.

-  An escape sequence does not always instruct the interpreter to display the character immediately following the backslash, for example the escape sequence \t causes a tab space to be placed in the string. The escape sequence \n causes the part of the string after it to be placed on a new line.

what if you want to include a backslash character in a string as shown below?

aStr = "To escape a quote, preceded it with a backslash, like this \"";

Web Design Sections

RSS Feed RSS Feed

Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269