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

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


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