Welcome to Bucaro TecHelp!

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

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

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

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 \"";

RSS Feed RSS Feed



Follow Stephen Bucaro Follow @Stephen Bucaro


Web Design Sections

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