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_Literals.html
   Copyright (c) 2013 by Dr. Herong Yang, 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.
世界你好!

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

Data Types, Variables and Expressions

 Primitive Data Types - Numbers, Strings, and Booleans

 Numeric Value Literals

String Literials

 Declaring Variables - "var" Statements

 Operators and Expressions

 Operators and Expressions - Examples

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

 'jrunscript' - JavaScript Shell Command from JDK

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 References

 PDF Printing Version