JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.00

String Literials

This section provides descriptions on string literals and a tutorial example on how to use them in JavaScript source code.

To provide values of strings in JavaScript source code, you need to use string value literals following these rules:

1. String literals are sequences of Unicode characters.

2. String literals must be enclosed in single quotes ('...') or double quotes ("...").

3. Several characters must be preceded with the escaping character, backslash (\): \b, \f, \n, \r, \t, \v, \', \", and \\.

4. Latin-1 characters can be presented as hexadecimal escape sequences. For example, \xA9 represents the copyright symbol.

5. Unicode characters can be presented as Unicode escape sequences. For example, \u4E16 represents a Chinese character.

Here is a JavaScript tutorial example that shows you how to use string literals in JavaScript source code:

<html>
<!-- String_Literials.html
   Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
-->
<head><title>String Literals</title></head>
<body>
<pre>
<script type="text/javascript">
   document.write("Hello World!");
   document.write('\n');

   document.write('Herong\'s Tutorial');
   document.write('\n');

   document.write("Double quote (\")");
   document.write('\n');

   document.write("Copyright \xA9 2008.");
   document.write('\n');

   document.write("\u4E16\u754C\u4F60\u597D\uFF01");
   document.write('\n');
</script>
</pre>
</body>
</html>

The output of this sample JavaScript is:

Hello World!
Herong's Tutorial
Double quote (")
Copyright © 2008.
世界你好!

Sections in This Chapter

Primitive Data Types - Numbers, Strings, and Booleans

Numeric Value Literals

String Literials

Declaring Variables - "var" Statements

Operators and Expressions

Operators and Expressions - Examples

Dr. Herong Yang, updated in 2008
String Literials