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

Java Script Include a Backslash Character in a String

In Java Script strings, escape sequences are used to inform the interpreter that a character is not just a letter, but that special action should be taken. For example the escape sequence \n indicates that the string should continue on a new line. A backslash indicates the next character is part of an escape sequence.

But what if you actually want to include a backslash in a string, not to start an escape sequence, but to just be displayed as a backslash? To print an actual backslash, you have to escape the backslash with a backslash, as shown below.

var aStr = "To include a backslash in a string, escape it with a \\";

In this case, the backslash would be displayed because it's actually part of an instruction to the interpreter to display a backslash.

In the example below, the programmer's intent was to display a message informing the user that the file was copied to the \networking folder.

alert("File copied to the \networking folder");

Where the heck is the etworking folder?

Instead, it informs the user that the file was copied to the etworking folder. Where the heck is the etworking folder? The error is caused because the \n in \networking is interpreted as an escape character instructing the interpreter to continue on a new line.

In the example below, the backslash was properly escaped.

alert("File copied to the \\networking folder");

The backslash was properly escaped

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