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

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

More Java Script Code:
• Use Escape to Replace Dangerous Characters
• Accessing Web Page Elements
• JavaScript Operators
• The if/else Structure
• Java Script Include a Backslash Character in a String
• Extract Part of a String
• The Java Script window.open Method
• Window onload Event
• Find a Character or a Substring Within a String
• The Location Object

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