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:

<!-- String_Literals.html
   Copyright (c) 2013 by Dr. Herong Yang,
<head><title>String Literals</title></head>
<script type="text/javascript">
   document.write("Hello World!");

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

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

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


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


 PDF Printing Version