JavaScript Special Characters

Wednesday, April 18, 2012

Just about every programming language has a list of special characters. These are characters that have a special purpose. JavaScript is no different. In JavaScript you can add special characters to a text string by prefixing the string with the backslash character.

in the following example, we will see how the use of special characters are needed to properly display a message.


var txt="Did you read that really "cool" book?";
document.write(txt);


In JavaScript single or double quotes are used to store the value of a string. Since we placed quotes around the word cool, the full string value is not assigned to the variable. If we require the quotes in our output, we need to use special characters in our code. Here is how we would address that need.


var txt="Did you read that really \"cool\" book?";
document.write(txt);


The following is a list of the most common special characters that you will be using when working with JavaScript code. Aside from the single and double quotes, the new line special character is also very common.


CodeOutput
\'Single quote
\"Double quote
\\Backslash
\bBackspace
\fForm feed
\nNew line
\rCarriage return
\tTab
\vVertical tab

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

HTML CSS and JavaScript Editor JavaScript and jQuery: The Missing Manual Professional JavaScript for Web Developers