In JavaScript you can add special characters to a text string by using the backslash sign.
The backslash (\) is used to insert apostrophes, new lines, quotes, and other special characters into a text string.
Look at the following JavaScript code:
var
txt="Sun Microsystems is the developer of "Java" programming language." |
In JavaScript, a string is started and stopped with either single or double quotes. This means that the string above will be chopped to: Sun Microsystems is the developer of
To solve this problem, you must place a backslash (\) before each double quote in "Viking". This turns each double quote into a string literal:
var
txt="Sun Microsystems is the developer of \"Java\" programming language." |
JavasScript will now output the proper text string: Sun Microsystems is the developer of "Java" programming language.
Here is another example:
document.write("Java \& JavaScript are two completely different languages.") |
The example above will produce the following output:
Java & JavaScript are two completely different languages. |
The table below lists other special characters that can be added to a text string with the backslash sign:
Code |
Output |
\' | single quote |
\" | double quote |
\& | ampersand |
\\ | backslash |
\n | new line |
\r | carriage return |
\t | tab |
\b | backspace |
\f | form feed |
To see how HTML and JavaScript work, you can only push the submit button, or you can make your own HTML and JavaScript code.