Tutorials

Learn web design and programming with our free video and text tutorials.

Special Characters in Javascript

Javascript allows you to use special characters such as quotes, apostrophes and new lines to a text string by using the backslash (\) sign.

In JavaScript, a string is started and stopped with either single quotes or double quotes. This causes the javascript below to only display: "And he started with "

<script type="text/javascript" language="javascript">
var txt="And he started with "only time will tell" to begin his speech.";
document.write(txt);
</script>

Javascript allows you to display a special character, in this case the double-quote ("), as part of the text string by placing a backslash (\) in front of the special character.

<script type="text/javascript" language="javascript">
var txt="And he started with \"only time will tell\" to begin his speech.";
document.write(txt);
</script>

The javascript code above display the entire text.

The table below lists The table below lists other special characters that can be added to a text string by using the backslash sign.

character outputs
' single quote
" double quote
& ampersand
\ backslash
n new line
r carriage return
t tab
b backspace
f form feed