100% found this document useful (1 vote)
11 views

JavaScript: A Beginner's Guide, Fifth Edition Pollock - The ebook in PDF format with all chapters is ready for download

The document promotes the ebook 'JavaScript: A Beginner's Guide, Fifth Edition' by John Pollock, available for download at ebookmass.com. It includes links to additional recommended ebooks and provides details about the author and technical editor. The document also contains copyright information and terms of use for the ebook.

Uploaded by

zoghamarkiz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
11 views

JavaScript: A Beginner's Guide, Fifth Edition Pollock - The ebook in PDF format with all chapters is ready for download

The document promotes the ebook 'JavaScript: A Beginner's Guide, Fifth Edition' by John Pollock, available for download at ebookmass.com. It includes links to additional recommended ebooks and provides details about the author and technical editor. The document also contains copyright information and terms of use for the ebook.

Uploaded by

zoghamarkiz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Visit ebookmass.

com to download the full version and


explore more ebook or textbook

JavaScript: A Beginner's Guide, Fifth Edition


Pollock

_____ Click the link below to download _____


https://ebookmass.com/product/javascript-a-beginners-guide-
fifth-edition-pollock/

Explore and download more ebook or textbook at ebookmass.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

JavaScript A Beginner's Guide Fifth Edition John Pollock

https://ebookmass.com/product/javascript-a-beginners-guide-fifth-
edition-john-pollock/

Javascript: Crash Course - The Complete Beginners Guide To


Learning JavaScript Programming In No Time! (Angularjs,
jQuery, JavaScript Visually) Nathaniel Hill
https://ebookmass.com/product/javascript-crash-course-the-complete-
beginners-guide-to-learning-javascript-programming-in-no-time-
angularjs-jquery-javascript-visually-nathaniel-hill/

The Norton Field Guide to Writing (Fifth Edition)

https://ebookmass.com/product/the-norton-field-guide-to-writing-fifth-
edition/

The Norton Field Guide to Writing: with Readings (Fifth


Edition) Fifth Edition – Ebook PDF Version

https://ebookmass.com/product/the-norton-field-guide-to-writing-with-
readings-fifth-edition-fifth-edition-ebook-pdf-version/
Creating Effective Teams: A Guide for Members and Leaders
(NULL) Fifth

https://ebookmass.com/product/creating-effective-teams-a-guide-for-
members-and-leaders-null-fifth/

Technical Analysis Explained, Fifth Edition : The


Successful Investoru2019s Guide

https://ebookmass.com/product/technical-analysis-explained-fifth-
edition-the-successful-investors-guide/

Learning Web Design: A Beginner’s Guide to HTML, CSS,


JavaScript, and Web Graphics 5th Edition, (Ebook PDF)

https://ebookmass.com/product/learning-web-design-a-beginners-guide-
to-html-css-javascript-and-web-graphics-5th-edition-ebook-pdf/

English File Beginner. Teacher's Guide (for speakers of


Spanish) Fourth Edition Christina Latham-Koenig

https://ebookmass.com/product/english-file-beginner-teachers-guide-
for-speakers-of-spanish-fourth-edition-christina-latham-koenig/

Writing Conversational Korean Book Two Katarina Pollock

https://ebookmass.com/product/writing-conversational-korean-book-two-
katarina-pollock/
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Folio: i

JavaScript
A Beginner’s Guide

Fifth Edition
John Pollock

New York Chicago San Francisco


Athens London Madrid Mexico City
Milan New Delhi Singapore
Sydney Toronto

00-FM.indd 1 17/09/19 5:33 PM


Copyright © 2020 by McGraw-Hill Education (Publisher). All rights reserved. Except as permitted under the United States
Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored
in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program
listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication.

ISBN: 978-1-26-045769-8
MHID: 1-26-045769-9

The material in this eBook also appears in the print version of this title: ISBN: 978-1-26-045768-1,
MHID: 1-26-045768-0.

eBook conversion by codeMantra


Version 1.0

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trade-
marked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringe-
ment of the trademark. Where such designations appear in this book, they have been printed with initial caps.

McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions or for use in
corporate training programs. To contact a representative, please visit the Contact Us page at www.mhprofessional.com.

Oracle and Java are registered trademarks of Oracle Corporation and/or its affiliates. All other trademarks are the property of
their respective owners, and McGraw-Hill Education makes no claim of ownership by the mention of products that contain these
marks.

Screen displays of copyrighted Oracle software programs have been reproduced herein with the permission of Oracle Corpora-
tion and/or its affiliates.

Information has been obtained by Publisher from sources believed to be reliable. However, because of the possibility of human
or mechanical error by our sources, Publisher, or others, Publisher does not guarantee to the accuracy, adequacy, or completeness
of any information included in this work and is not responsible for any errors or omissions or the results obtained from the use
of such information.

TERMS OF USE

This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use of this work
is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the
work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit,
distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill Education’s prior consent. You
may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to
use the work may be terminated if you fail to comply with these terms.

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO GUARANTEES
OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED
FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA
HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUD-
ING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE. McGraw-Hill Education and its licensors do not warrant or guarantee that the functions contained in the work will
meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill Education nor its licensors
shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages
resulting therefrom. McGraw-Hill Education has no responsibility for the content of any information accessed through the work.
Under no circumstances shall McGraw-Hill Education and/or its licensors be liable for any indirect, incidental, special, punitive,
consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of
the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or
cause arises in contract, tort or otherwise.
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Foli iii

To my wife, Heather, and children, Eva, Elizabeth, Elaine, and Evan,


Bruce and Joy Anderson, and Dr. J. D. and Linda Andrews

In memory of John and Betty Hopkins, James D. and


Livian Anderson, John William and Edith Hopkins,
Burley T. and Aline Price, “Doc” Flores, and Clifton Idom

00-FM.indd 3 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Foli iv

About the Author


John Pollock is employed as a software developer during
the day and works on Web sites and other projects during
the evening. You can find him on Twitter (@ScripttheWeb)
or LinkedIn (https://www.linkedin.com/in/john-pollock-
82a2b074). John holds a Bachelor of Arts in English from Sam
Houston State University and currently lives in New Waverly,
Texas with his wife, Heather, and children, Eva, Elizabeth,
Elaine, and Evan.

About the Technical Editor


Christie Sorenson is a senior software engineer at ZingChart.
She has worked on JavaScript-based systems since 1997 and
has been fascinated with the evolution of the language. She
has collaborated and been the technical editor on several
JavaScript and HTML books. She holds a Bachelor of Science
in Computer Science from University of California, San Diego,
and now lives in San Francisco with her husband, Luke, and
daughters, Ali and Keira.

00-FM.indd 4 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents
ACKNOWLEDGMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi
.
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
.
1 Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
.
What You Need to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
.
Basic HTML and CSS Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
.
Basic Text Editor and Web Browser Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
.
Which Version? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
.
Client-Side and Server-Side Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
.
Beginning with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Prototype-Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Interpreted Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Numerous Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
.
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
.
Try This 1-1: Use JavaScript to Write Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
.
Chapter 1 Self Test ................................................................. 11
.
2 Placing JavaScript in an HTML File ..................................... 15
.
Using the HTML Script Tags ....................................................... 16
.
Identifying the Scripting Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
.
Calling External Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
.
v

00-FM.indd 5 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

vi JavaScript: A Beginner’s Guide

Specifying when the Script Should Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

.
Using <noscript></noscript> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

.
Creating Your First Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

.
Writing a “Hello World” Script ............................................... 20

.
Creating an HTML Document for the Script .................................. 21

.
Inserting the Script into the HTML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

.
Try This 2-1: Insert a Script into an HTML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

.
Using External JavaScript Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

.
Creating a JavaScript File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

.
Creating the HTML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

.
Viewing the Pages in Your Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

.
Try This 2-2: Call an External Script from an HTML Document . . . . . . . . . . . . . . . . . . . . . 27

.
Using JavaScript Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
.
Inserting Comments on One Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
.
Adding Multiple-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
Chapter 2 Self Test ................................................................. 30
.
3 Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Understanding Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
.
Why Variables Are Useful .......................................................... 35
.
Variables as Placeholders for Unknown Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
.
Variables as Time-Savers ..................................................... 35
.
Variables as Code Clarifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
.
Defining Variables for Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
.
Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
.
Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
.
Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
.
Understanding Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
.
Number ...................................................................... 41
.
String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
.
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Undefined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
.
Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
.
Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
.
Try This 3-1: Declare Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
.
Using Variables in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Making a Call to a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Adding Variables to Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
.
Writing a Page of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Creating the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Defining the Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
.
Adding the Commands ....................................................... 55
.
Modifying the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
.
00-FM.indd 6 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents vii

Try This 3-2: Create an HTML Page with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

.
Chapter 3 Self Test ................................................................. 60

.
4 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

.
What a Function Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

.
Why Functions Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

.
Structuring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

.
Declaring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

.
Defining the Code for Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

.
Naming Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
.
Adding Arguments to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

.
Adding Return Statements to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

.
Calling Functions in Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
.
Script Tags: Head Section or Body Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

.
Calling a Function from Another Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

.
Calling Functions with Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
76
Calling Functions with Return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
.
Other Ways to Define Functions .............................................. 82
.
Try This 4-1: Create an HTML Page with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
.
Scope/Context Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
.
Global Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
.
Function Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.
Block Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
.
Try This 4-2: Write Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
.
Chapter 4 Self Test ................................................................. 91
.
5 JavaScript Operators ..................................................... 95
.
Understanding the Operator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
.
Understanding Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
.
The Addition Operator (+) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
.
The Subtraction Operator (–) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
.
The Multiplication Operator (*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
.
The Division Operator (/) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
.
The Modulus Operator (%) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
.
The Increment Operator (++) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
.
The Decrement Operator (– –) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
.
The Unary Plus Operator (+) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
.
The Unary Negation Operator (–) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
.
The Exponentiation Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
.
Understanding Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
.
The Assignment Operator (=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
.
The Add-and-Assign Operator (+=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
.
The Subtract-and-Assign Operator (–=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
.
The Multiply-and-Assign Operator (*=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
.
The Divide-and-Assign Operator (/=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
.
00-FM.indd 7 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

viii JavaScript: A Beginner’s Guide

The Modulus-and-Assign Operator (%=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

.
The Exponent-and-Assign Operator (**=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

.
Try This 5-1: Adjust a Variable Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

.
Understanding Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

.
The Is-Equal-To Operator (==) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

.
The Is-Not-Equal-To Operator (!=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

.
The Strict Is-Equal-To Operator (===) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

.
The Strict Is-Not-Equal-To Operator (!==) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

.
The Is-Greater-Than Operator (>) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

.
The Is-Less-Than Operator (<) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

.
The Is-Greater-Than-or-Equal-To Operator (>=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

.
The Is-Less-Than-or-Equal-To Operator (<=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

.
Understanding Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
117
The AND Operator (&&) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
.
The OR Operator (||) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
.
The NOT Operator (!) ........................................................ 118
.
The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
.
Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
.
Understanding Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
.
Try This 5-2: True or False? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
.
Chapter 5 Self Test ................................................................. 123
.
6 Conditional Statements and Loops ....................................... 125
.
Defining Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
.
What Is a Conditional Statement? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
.
Why Conditional Statements Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using if/else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
.
Using the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
.
Using the Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
.
User Input from a Prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
.
Try This 6-1: Work with User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
.
Defining Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
.
What Is a Loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
.
Why Loops Are Useful ....................................................... 144
.
Using Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
.
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
.
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
.
do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
.
for in, for each in, and for of . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
.
Using break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
.
Try This 6-2: Work with for Loops and while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
.
Chapter 6 Self Test ................................................................. 160
.
00-FM.indd 8 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents ix

7 JavaScript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

.
What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

.
Why Arrays Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

.
Defining and Accessing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

.
Naming an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

.
Defining an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Accessing an Array’s Elements ............................................... 167

.
Using the length Property and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

.
Changing Array Values and Changing the Length ............................. 169

.
Try This 7-1: Use Loops with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

.
Array Properties and Methods ...................................................... 172
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
.
Nesting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
.
Defining Nested Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
.
Loops and Nested Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
.
Try This 7-2: Nested Arrays Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
.
Chapter 7 Self Test ................................................................. 193
.
8 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
.
Defining Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.
Single Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
.
Try This 8-1: Create a Computer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
.
Object Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
.
Constructor Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
.
Using Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
.
The class Keyword ........................................................... 209
.
Helpful Statements for Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
.
The for-in Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
.
The with Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
.
Try This 8-2: Practice with the Combination Constructor/Prototype Pattern . . . . . . . . . . 212
.
Understanding Predefined JavaScript Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
.
The Navigator Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
.
The History Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
.
Chapter 8 Self Test ................................................................. 218
.
9 The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
.
Defining the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
.
Using the Document Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Using the Properties of the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
.
The cookie Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
.
00-FM.indd 9 17/09/19 5:33
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

x JavaScript: A Beginner’s Guide

The dir Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

.
The lastModified Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

.
The referrer Property ......................................................... 227

.
The title Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

.
The URL Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

.
The URLUnencoded Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Using the Methods of the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

.
The get Methods for Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

.
The open() and close() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

.
The write() and writeln() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

.
Using DOM Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
.
DOM Node Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
238
DOM Node Methods ......................................................... 241
.
Try This 9-1: Add a DOM Node to the Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246

.
Creating Dynamic Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
.
Styles in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
.
Simple Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
.
Coding a Dynamic Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
.
Try This 9-2: Try Out Property Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Chapter 9 Self Test ................................................................. 253
.
10 Event Handlers ........................................................... 255
.
What Is an Event Handler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
.
Why Event Handlers Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
.
Understanding Event Handler Locations and Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
.
Using an Event Handler in an HTML Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
.
Using an Event Handler in the Script Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
.
Learning the Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
.
The Click Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
.
Focus and Blur Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
.
The Load and Unload Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
.
The Reset and Submit Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
.
The Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
.
The Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
.
Try This 10-1: Focus and Blur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
.
Other Ways to Register Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
.
The addEventListener() Method .............................................. 272
.
The attachEvent() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
.
The Event Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
.
DOM and Internet Explorer: DOM Level 0 Registration . . . . . . . . . . . . . . . . . . . . . . . 274
.
Using event with Modern Event Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
.
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
.
Event Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
.
Try This 10-2: Using addEventListener() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
.
00-FM.indd 10 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents xi

Creating Scripts Using Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

.
Show Hidden Content ........................................................ 279

.
Change Content .............................................................. 280

.
Custom Events ............................................................... 284

.
Chapter 10 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

.
11 Introduction to Node.js ................................................... 289

.
Introducing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
.
Installing Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
.
Check for a Current Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

.
Install Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
.
Write a “Hello World” Script ................................................. 292

.
Using Node Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
.
Using Native Node Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
295
Asynchronous Execution ..................................................... 296
.
Non-Native Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
.
Try This 11-1: Use a Custom Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
.
Installing a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
.
Database Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
.
Install PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
.
Create a Database Using pgAdmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
.
Try This 11-2: Test Some SQL Queries ............................................. 312
.
Creating a Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
.
Chapter 11 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
.
12 Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
.
Using the Math Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
What Is the Math Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
How the Math Object Is Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
.
Try This 12-1: Display a Random Link on a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
.
Understanding the Number Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
.
Using the Date Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
.
Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
.
Methods That Get Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
.
Methods That Set Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
.
Other Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
.
How About Some Date Scripts? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
.
Try This 12-2: Create a JavaScript Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
.
Continuing Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
.
Getting to the Needed Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
.
Running Some Calculations on the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
.
Chapter 12 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
.
00-FM.indd 11 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

xii JavaScript: A Beginner’s Guide

13 Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357

.
Introduction to the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

.
The String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

.
The String Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

.
What’s the Difference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

.
Using the Properties and Methods of the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

.
The length Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

.
Methods of the String Object ....................................................... 360

.
Try This 13-1: Use indexOf() to Test an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370

.
Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
.
Setting a Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
.
Reading a Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
.
Try This 13-2: Remember a Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
376
Using Regular Expressions ......................................................... 377
.
Creating Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
.
Testing Strings Against Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378

.
Adding Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
.
Creating Powerful Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
.
Grouping Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
.
The replace(), match(), matchAll(), and search() Methods . . . . . . . . . . . . . . . . . . . . . 384

.
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
.
Continuing Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
.
Chapter 13 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
.
14 Browser-Based JavaScript ................................................ 391
.
Window: The Global Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
.
Using the Properties of the Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
.
The closed Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
The frames Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The innerWidth and innerHeight Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The length Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The location Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
.
The name Property ........................................................... 396
.
The opener Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
.
The parent, self, and top Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
.
The status and defaultStatus Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
.
Try This 14-1: Use the location and innerWidth Properties .......................... 398
.
Using the Methods of the Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
.
The alert(), prompt(), and confirm() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
The print() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
.
The setInterval() and clearInterval() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
The setTimeout() and clearTimeout() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
.
Try This 14-2: Use the setTimeout() and confirm() Methods . . . . . . . . . . . . . . . . . . . . . . . . . 406
.
00-FM.indd 12 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents xiii

The Main Window and New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

.
The Tale of Pop-up Windows ................................................. 407

.
Opening New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408

.
Closing New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

.
Moving, Resizing, and Scrolling New Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

.
The resizeBy() and resizeTo() Methods ....................................... 416

.
The scrollBy() and ScrollTo() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418

.
Working with Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
.
Rollovers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
.
JavaScript and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
420
Purpose of Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
.
Accessing Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
.
Breaking Out of Frames ...................................................... 423
.
Using iFrames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
.
Chapter 14 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
.
15 JavaScript Forms and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
.
Accessing Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
.
Using the forms Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
.
Using an ID .................................................................. 431
.
Using the Properties and Methods of the Form Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
.
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
.
Ensuring the Accessibility of Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
.
Using Proper Element and Label Order ....................................... 438
.
Using <label></label> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
.
Using <fieldset></fieldset> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
.
Not Assuming Client-Side Scripting .......................................... 439
.
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
.
Simple Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
.
Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
.
Check Boxes and Radio Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
.
Try This 15-1: Request a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
.
HTML5 and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
.
New Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
.
New Input Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
.
New Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
.
HTML5 Form Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
.
Try This 15-2: Validate a Phone Number with HTML5 or JavaScript ................ 455
.
AJAX and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
.
AJAX ........................................................................ 456
.
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
.
Chapter 15 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
.
00-FM.indd 13 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

xiv JavaScript: A Beginner’s Guide

16 Further Browser-Based JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469

.
Using jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

.
Obtaining jQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

.
Getting Started: document.ready() ............................................ 471

.
Using Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

.
Altering Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

.
Methods for Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475

.
Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
.
Try This 16-1: Use jQuery to Create Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

.
Debugging Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
.
Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
.
Using the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
.
Using a Lint Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
.
Browser Developer Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
.
JavaScript and Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
.
Separate Content from Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
486
Enhancing Content ........................................................... 488
.
Try This 16-2: Make This Code Accessible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
.
JavaScript Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
.
Page Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
.
JavaScript and APIs from HTML ................................................... 492
.
The <canvas> Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
.
Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
.
Try This 16-3: Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
.
Node.js App Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
.
Update the Node.js Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Update the Front-end Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
.
Need Help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
.
Chapter 16 Self Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
.
A Answers to Self Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
.
Chapter 1: Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
.
Chapter 2: Placing JavaScript in an HTML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
.
Chapter 3: Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
.
Chapter 4: Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
.
Chapter 5: JavaScript Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
.
Chapter 6: Conditional Statements and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
.
Chapter 7: JavaScript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
.
Chapter 8: Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
.
Chapter 9: The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
.
00-FM.indd 14 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Contents xv

Chapter 10: Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

.
Chapter 11: Introduction to Node.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518

.
Chapter 12: Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

.
Chapter 13: Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

.
Chapter 14: Browser-Based JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520

.
Chapter 15: JavaScript Forms and Data ....................................... 521

.
Chapter 16: Further Browser-Based JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

.
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

.
00-FM.indd 15 17/09/19 5:33
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter
Blind Folio vi

Acknowledgments
I would like to begin by thanking my wonderful wife, Heather Pollock, for all of her love,
support, and encouragement in all I do. I love you! I would also like to thank my three
daughters, Eva, Elizabeth, and Elaine, as well as my son, Evan. I love all of you!
I would like to thank my parents, Bruce and Joy Anderson, for their love and guidance, and
for always supporting my endeavors.
I would like to thank Dr. J. D. and Linda Andrews for their love, guidance, and support.
In addition, I would like to thank Richard Pollock (brother) and family, Misty Castleman
(sister) and family, Warren Anderson (brother) and family, Jon Andrews (brother) and family,
Lisa and Julian Owens (aunt/uncle) and family, and every aunt, uncle, cousin, or other relation
in my family. All of you have been a great influence in my life.
I would like to thank all of my editors at McGraw-Hill for their outstanding help and
support throughout the writing of this book. Thanks to Lisa McClain, Emily Walters, Claire Yee,
Snehil Sharma, Sarika Gupta, Bart Reed, and to all the editors who worked on this and previous
editions of the book.
Thanks to my technical editor, Christie Sorenson, for editing and checking over all the
technical aspects of the book and for helping me provide clear explanations of the topics that
are covered.
I would like to thank God for the ability He has given me to help and teach people by my
writing. “In all your ways acknowledge Him, and He shall direct your paths.” (Proverbs 3:6)

xvi

00-FM.indd 16 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

Introduction
W elcome to JavaScript: A Beginner’s Guide, Fifth Edition! Years ago, I was surfing
the Web and noticed that people were publishing pages about themselves and calling
them homepages. After viewing a number of these, I decided to create a homepage myself. I
had no idea where to begin, but through trial and error I figured out how to code HTML and
publish my documents on a Web server. Over time, I saw some interesting effects used on other
homepages (like alert messages that popped up out of nowhere or images that would magically
change when I moved my mouse over them). I was curious and just had to know what was being
done to create those effects. Were these page creators using HTML tags I did not know about?
Eventually, one site revealed what was being used to create those effects: JavaScript. I
went in search of information on it and came across a few tutorials and scripts on the Web.
Since I had programmed in other languages (such as a relatively obscure language called Ada),
I was able to catch on to JavaScript fairly quickly by looking at these tutorials and scripts.
I learned enough that I decided to create a Web site that would teach HTML and JavaScript
to beginners. As soon as I began the project, I received questions from visitors that were
way over my head—forcing me to dig deeper and learn more about JavaScript. As a result,
I became completely familiar with this scripting language and what it can do. Not only can
you add fun effects to a Web page, you can create scripts that will perform useful tasks, like
validate form input, add navigational elements to documents, and react to user events.
The goal of this book is to help you to learn the basics of the JavaScript language with as
little hair pulling and monitor smashing as possible. You do not need any prior programming
experience to learn JavaScript from this book. All you need is knowledge of HTML and/or
XHTML, Cascading Style Sheets (CSS), and how to use your favorite text editor and Web
browser (see Chapter 1 for more information).

xvii

00-FM.indd 17 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

xviii JavaScript: A Beginner’s Guide

What This Book Covers


The 16 chapters of this book cover specific topics on the JavaScript language. The first two
chapters cover the most basic aspects of the language: what it is, what you need to know to
begin using JavaScript, and how to place JavaScript into an HTML file. The middle of the
book (Chapters 3–14) covers beginning JavaScript topics from variables all the way to using
JavaScript with forms. The final two chapters (Chapters 15–16) introduce some advanced
techniques, and point you toward resources if you want to learn more about JavaScript once
you have completed the book.
This book includes a number of special features in each chapter to assist you in learning
JavaScript. These features include

● Key Skills & Concepts Each chapter begins with a set of key skills and concepts that
you will understand by the end of the chapter.
● Ask the Expert The Ask the Expert sections present commonly asked questions about
topics covered in the preceding text, with responses from the author.
● Try This These sections get you to practice what you have learned using a hands-on
approach. Each Try This will have you code a script through step-by-step directions
on what you need to do to in order to accomplish the goal. You can find solutions to
each project on the McGraw-Hill Professional Web site at www.mhprofessional.com/
computingdownload.
● Notes, Tips, and Cautions These elements call your attention to noteworthy statements
that you will find helpful as you move through the chapters.
● Code Code listings display example source code used in scripts or programs.
● Callouts Callouts display helpful hints and notes about the example code, pointing to the
relevant lines in the code.
● Self Test Each chapter ends with a Self Test, a series of 15 questions to see if you have
mastered the topics covered in the chapter. The answers to each Self Test can be found in
the appendix.

That is it! You are now familiar with the organization and special features of this book to
start your journey through JavaScript. If you find that you are stuck and need help, feel free to
contact me with your questions. To contact me, you can reach me on LinkedIn (https://www
.linkedin.com/in/john-pollock-82a2b074) or you can find me on Twitter (@ScripttheWeb).
Now it is time to learn JavaScript. Get ready, get set, and have fun!

00-FM.indd 18 17/09/19 5:33


BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0

Chapter 1
Introduction to JavaScript

01-ch01.indd 1 27/09/19 9:56


Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Other documents randomly have
different content
and advanced rank as the means of attaining these objects of
ambition. He concludes this letter with some strong observations on
the advantages enjoyed by the Company's army, and the determined
resolution of the Committee not to yield to unreasonable demands.
"I am very glad," he observes, "the officers have been so prudent as
to lay aside their intentions of presenting a memorial; and the
Company and myself are much obliged to Sir R. Barker for his
soldier-like conduct and behaviour upon the occasion; although, I
can assure you, there has been no memorial presented from the
other brigades on the like subject. It is true, the Governor and
Committee have received a remonstrance from the officers of
Colonel Smith's brigade, setting forth the dearness of provisions and
all other necessaries, at that great distance; and we have, in
consequence of its being only a temporary expense to the Company,
agreed to let things remain on their present footing, until the
brigade be withdrawn from Sujah Dowlah's dominions; but the
officers are at the same time informed, in the most positive terms,
that this indulgence will cease the instant the troops cross the
Carumnapa.
"I need not repeat how positive and absolute the Company's orders
from Europe are, about reducing the military expenses to the
proportion of the establishments on the coast of Coromandel, nay,
even to less, because they imagine the price of provisions is lower;
and I believe you are not unacquainted that the officers, for the first
year they served in Bengal, were all satisfied with single batta: the
double batta was merely an indulgence obtained by me, and came
immediately out of the Nabob's own pocket. It has been continued
ever since, by the authority of the King's officers; and the Governor
and Council have been obliged to acquiesce, in opposition to
frequent orders from the Court of Directors. In short, our military
gentlemen (countenanced and supported by the King's officers, who
over-awed the Governor and Council in such a manner, that, had
quadruple batta been demanded, I doubt whether it would have
been refused,) have continued to receive the indulgence almost as a
matter of right. But now that the Company have appointed so many
field officers, who are immediately in their own service, the salutary
effects are visible; and I hope not only to see the strictest discipline
and subordination enforced, but likewise economy established; for
unless luxury and extravagance be abolished, discipline must fail,
and the officers and soldiers be rendered incapable of doing their
duty to their country and the Company. My grand object, you know,
is, that none under the rank of field officers should have money to
throw away. When they arrive at that rank, their hands are filled
with such large advantages, that they may be certain of acquiring an
independency in a few years. This consideration might, one would
imagine, induce the officers to rest satisfied with their present
appointments, since they have a greater advantage in prospect than
they ever enjoyed before. My dear Colonel, let them look at all other
services. Is there such another service in the world as this is, upon
the footing on which it now stands? In the West Indies, in America,
where all European articles and provisions likewise are full as dear as
in Bengal, the officers and soldiers have scarce any extraordinary
allowances at all. Let them reflect for a moment on the miserable
condition of the half-pay officers in England, many of whom have
undergone dangers and fatigues much superior to any in India. How
many of them would be glad to serve the Company on their own
terms? Add to this, that the revenues of Bengal, Bahar, and Orissa
belong to the Company themselves, and not to the Nabob; that what
is now paid comes immediately out of their own pockets; and
although the officers may plead prescription for indulgences from
the Nabob, they can plead no such prescription from the Company. I
may indeed assure you, the Committee are resolutely determined
upon carrying the Company's orders into execution, and enforcing
the new regulations. The only indulgence they could reconcile to
their duty to the Company, was the extra allowance of 40 rupees per
diem to each commanding officer of the brigades when in garrison,
in order that his table might be a help to the other officers, till a
moderate way of living had taken place."
Lord Clive, it has been before mentioned, had received a legacy of
five lacs of rupees bequeathed to him by the Nabob Meer Jaffier,
which he had lodged in the Company's treasury, in order that it
might be converted into a fund for the relief of disabled or decayed
European officers and soldiers, and of destitute widows of officers of
the Company's army. Noticing, in a letter to Sir R. Fletcher[43], an
absurd combination which the officers of his brigade had entered
into, against Captain Ducarell and his brothers, he alludes to this
munificent act. "On my arrival at Monghyr," (he observes) "I shall
consider of some effectual means to put a stop to such unjust
resentment." "I have," (he adds) "at present, a scheme on foot for
the benefit of officers in the Bengal establishment; but I shall not
hesitate to exclude any whom I may think undeserving, in any
respect soever."
On the 28th of April, 1766, Lord Clive intimated to Sir R. Fletcher, his
decided resolution to treat with the utmost severity, all who
proceeded to the extreme of resigning the service; and, on the 29th
of April, 1766, he makes the following observations in a letter to Sir
R. Barker:—
"During your absence upon the Bettoa expedition, the captains,
lieutenants, and ensigns of your brigade have preferred a very
extraordinary remonstrance to the Governor and Council, upon the
subject of batta. This proceeding was, I think, somewhat
contemptuous towards you; and, as the remonstrance was not
transmitted through the channel of the commanding officer, I have
given it as my opinion, that the Board should pay no other attention
to it than that of sending it to you for your information. By the
behaviour of our present corps of officers, one would actually
conclude that every true idea of military discipline was effaced. I
have just received authentic intelligence, that no less than one
hundred and thirty officers of the third brigade have deposited their
commissions, and entered into an association not to serve, unless
the double batta be restored. To this, it seems, is added an
agreement to subscribe for the maintenance of the principals (who
they imagine will be the only sufferers), till their arrival in Europe,
and to purchase for them commissions of equal rank in his Majesty's
service. Can any man in his senses imagine that the Secretary at
War, being made acquainted with the names and conduct of these
officers, will ever give his consent to their admission into the King's
regiments? With regard to these who have already served in Europe,
and are now upon the half-pay list, they would do well to recollect
that they will not be entitled to their half-pay, on their return to
England, without producing a certificate of their good behaviour in
the service of the Company; for such is his Majesty's declared
resolution; and if they cannot obtain half-pay, how can they expect
to be admitted upon full pay? The enclosed copy of a letter we have
just despatched to the gentlemen of Council at Fort William, will
inform you of the measure that must take place, if this unmilitary
association be not dissolved. And I will add, for my own part, that
any officer who resigns his commission, from no other cause of
disgust than the Company's orders for the reduction of batta, shall
be absolutely dismissed the service, and never restored. For their
own sakes, I hope they will speedily resolve upon a change of
conduct, and I doubt not you will use your utmost influence to bring
them to a right sense of their duty. If my sentiments have any
weight, you are at liberty to make this as public as you please, as
also the copy of the letter to the Council."
Lord Clive appears, throughout the whole of the violent proceedings
of the officers, to have been convinced that the civil servants of the
Company had instigated and aided the military in their mutinous
acts; and in answer to a letter from Mr. Verelst[44], he observes,
—"With regard to the assistance you say we may have from the
civilians on an emergency, I can never consent to receive it, as I am
certain they were the original cause of this mutinous association."
When he learnt that the combination of officers had taken a
formidable shape, he thus expresses himself, in a letter[45] to the
same gentleman:—"I am determined to leave this place
(Moorshedabad) on Tuesday next, and proceed with all expedition to
Monghyr. Our business at this city, material as it is, must for a little
while give way to the more pressing occasion which requires our
presence at the army. If I find a fair opportunity, my endeavours to
get some of the mutinous ringleaders shot will not be wanting. At all
events, most of them shall be dismissed the service."
Lord Clive, at this period, felt the greatest annoyance from the want
of cordial support from Mr. Sumner, the senior member of the
Committee; and, irritated as he was by the continued opposition of
several of the members of Council, the discontent of the civil, and
the violence of the military, we are not surprised at the first
paragraph of a private letter, written at this period to Mr. Palk,
Governor of Madras, under date the 6th of May, 1766. "Do you
think," he asks, "History can furnish an instance of a man, with
40,000l. per annum, a wife and family, a father and mother, brother
and sisters, cousins and relations in abundance, abandoning his
native country, and all the blessings of life, to take charge of a
government so corrupt, so headstrong, so lost to all principle and
sense of honour, as this is?"
When a communication from Sir Robert Barker led him to believe
that the officers would proceed to the last extreme, he wrote[46], as
we have already seen, in terms that strongly expressed his sense of
their conduct, and the resolution to which he had come in respect to
his own.
The heavy rains made Lord Clive's progress to Monghyr slow, but he
directed every act of the officers commanding brigades, and to each
of them he wrote private letters daily. He gave to Sir R. Fletcher and
Sir R. Barker powers to promise forgiveness to subalterns, but not
the captains, whom it was his fixed resolution to bring to condign
punishment, on the just ground that their better experience
rendered them less excusable than the young men to whom they
gave so evil an example. To Colonel Smith a greater latitude was
given. He was authorised, as has been stated, in the event of the
Mahrattas invading the country, but in no other case whatever, to
make terms with his insubordinate officers, if they tendered their
commissions.
On reaching Monghyr, he wrote to Colonel Smith[47] what had
occurred, explaining the riotous conduct of the officers on his
sending off some of the ringleaders to Calcutta, and the
apprehended mutiny of the soldiers, which occurred from their
expecting that their commanders were to head them. With respect
to the native troops, he observes, "The black Sepoy officers, as well
as men, have given great proofs of fidelity and steadiness upon this
occasion; and, so long as they remain so, nothing is to be
apprehended from the European soldiery, even if they should be
mutinously inclined." In the conclusion of this letter, he desired
Colonel Smith to inform his brigade, of his having recently vested
70,000l., the legacy left him by Meer Jaffier, for the purpose of its
interest being applied for the relief of disabled, worn-out officers and
men of the Indian army.
In a letter dated the 4th of May, in which Lord Clive announced his
positive intention to Sir R. Fletcher of proceeding to Monghyr as
speedily as possible, he informs that officer that he is determined
"those who have been most active shall be dismissed the service."
Immediately on his arrival at Monghyr, he assembled the troops, and
harangued them with the best effect, as far as the men were
concerned. "I have this morning," he observes in a letter to Colonel
Smith[48], "had all the troops under arms, and made them a speech
on the occasion. The sepoys are very firmly attached to their duty;
and I am now confident that the Europeans likewise will give us no
cause of complaint or apprehension. In short, every thing here is as
quiet and as well regulated as could be wished."
From a letter to Mr. Verelst, Lord Clive appears to have learnt, in two
days after his arrival at Monghyr, of the encouragement given to the
officers by Sir R. Fletcher; a circumstance at the discovery of which
he expresses great astonishment. From considerations of prudence,
to which we have before alluded, he was, however, withheld from
acting in an affair of so delicate a nature, without complete
information. But his chief anxiety at this moment was the situation
of Colonel Smith, who occupied an advanced position on the frontier,
and had to apprehend an attack from the Mahrattas, at the very
moment his officers were threatening to leave him. Though Lord
Clive expressed great indignation at the conduct of these officers, he
appears to have contemplated forgiveness to those who had been
led by the influence of bad example, or by fears, weakness, and
inexperience, into a guilty association. In answer to a letter from
Colonel Smith[49], he observes, "The very infamous behaviour of so
many officers will be an everlasting reproach upon the English
nation, and cast a stain upon the Company's service, which all the
water of the Ganges can never wash away. The Court of Directors
will have before them a very convincing proof of the fatal effects of
donation money, and extravagant allowances, and will be reduced to
the necessity of taking some very extraordinary measures, to
prevent such dangerous steps being taken by the officers in future."
*****
"When the officers were turned out of Monghyr, and obliged to
embark, many of them went away with tears in their eyes, and saw
their crime in the proper light. Some were first frightened into the
measure, and then threatened with death if they retracted; others
were inveigled, and there is not the least doubt but we shall soon
have it in our option to choose from among the whole the most
moderate and deserving of those who have resigned the service. It
will shock you much to hear that there is great reason to imagine
that a very principal person at Monghyr has been the chief instigator
of this mutinous behaviour; and you will be still more surprised to
learn that the civilians have been very active in promoting the
association. All the officers at Monghyr affirm there has been a
subscription of 160,000 rupees made for that purpose. This last
circumstance I can scarce credit."
Alluding, in a subsequent letter to Colonel Smith[50], to the conduct
of the officers of his brigade, Lord Clive observes, "The behaviour of
those officers who were serving under you in the lines, and who
resigned their commissions almost in the face of an enemy, is so
very infamous, that no consideration on earth shall induce me to
restore one of them to the service." "I wish," he goes on to observe,
"with all my heart, your letter to the officers at Allahabad may
persuade some of them to return to their duty; though, from the
behaviour of the officers of the other brigades, I much doubt it. If
any thing can have effect, it will be the fate of the other officers, all
of whom I have sent down to Calcutta. Captain Stainforth and
Ensign Hoggan having expressed a proper sense of their late
misbehaviour, I have permitted them both to return to their duty;
and I have no doubt, from the surprise shown, and distress felt, at
my accepting all their commissions, as many of them will offer to
return to the service as we shall choose to accept of."
At the same time that these communications were made to Colonel
Smith, a number of the best officers of the army, who were
unassociated with the others, and had been called from different
quarters, were sent to Allahabad to render his field brigade efficient
for service; and those measures, added to the decided conduct of
the Colonel, made Clive deem his own presence at Allahabad
unnecessary; and he therefore determined to proceed no further
than Chuprah. He had received information that the officers in
garrison at Allahabad had left that place, after writing an impertinent
letter to Colonel Smith. Orders were immediately given to send
detachments of sepoys to make them prisoners, when they were
directed to be sent to Calcutta; and Mr. Sumner and the Committee
were requested[51] to keep them confined till vessels were ready to
sail for England, when he desired that they should be sent home.
Orders were given, upon this occasion, that the private letters by the
post from Allahabad should be stopped, and sent in a separate bag
to the post-master at Calcutta.
It would appear from the contents of several of his private letters of
the same date, that Clive did not apprehend the slightest danger to
the public interests from the defection of the officers at Allahabad.
Major Smith, who commanded that fortress, had exhibited much
spirit and firmness. His influence with the native troops gave him
great strength; and none of the officers in command of the sepoy
corps had joined the combination. Their situation was, at this period,
one of consequence and emolument; their influence with their men
great; and to this circumstance is, in a great degree, to be ascribed
the unshaken fidelity of the sepoys, on a confidence in whom, it may
be pronounced, the measures taken by Lord Clive, as well as those
of the officers commanding brigades, were chiefly grounded.
Anxiety of mind, fatigue of body, and the extreme heat of the
weather, affected Lord Clive's health; and, for one or two days, some
letters were written to various quarters, by his secretary, Mr. Henry
Strachey. And it is here to be remarked, as illustrative of the
character of this extraordinary man, that from the day he received
intimation of the discontent of the officers of the army, there are
entered in his letter-books never less than three, and sometimes
five, six, and seven letters, written daily by himself on the subject;
and the same books fully show that, during this period of great
mental and bodily exertion, he gave personally the most minute
attention to every other branch of public affairs. At the same time,
he does not appear to have neglected any private correspondents,
either in India or England.
On the 29th of May, Mr. Strachey informed Mr. Verelst, by Lord
Clive's desire, that a considerable number of officers had returned to
their duty, and desired to have their commissions restored; but that,
while Lord Clive was disposed to act with much lenity and
indulgence, it was far from his intention to accept the services of all
those by whom they had been proffered. Some of these his Lordship
(Mr. Strachey writes) is resolved to bring to justice.
"The only observation," he writes[52], "Lord Clive directs me to make
upon Mrs. W.'s intelligence about the rage of the civilians, and more
than madness of the military, is, that they have mutually encouraged
each other to such a degree of licentiousness, in defiance of civil and
martial law, that he hardly expects to see a change of sentiments till
the severity of example shall have convinced the settlement of his
resolution to save it from destruction."
Lord Clive wrote to Monsieur Law[53], the French Governor of
Chandernagore, and Monsieur Vernet, the Dutch Chief at Chinsurat,
on the subject of the officers who had deserted their duty; and,
while he informed them of the dishonourable course of action these
officers had pursued, he requested they should not receive or give
protection to men who had so dishonoured themselves. These
respectable individuals, to whom, both personally, and as
representatives of their country, Lord Clive always paid the greatest
attention and respect, acted on this, and on all occasions during the
time he was in India, in a manner suited to their own high
characters.
A short letter from Lord Clive to Mr. Sykes, resident at
Moorshedabad, of the 28th of May, in which he states the grounds
on which alone he would allow those officers who were repentant to
remain with their corps, and his steady resolution to admit of no
compromise, shows that he viewed the combination to be, at this
date, completely broken and subdued. "I have received," he
observes, "your favours of the 19th and 20th. Captains Cummings
and Mackenzie, having not yet absolutely declared their resolution to
keep or resign their commissions, I desire you will send for these
gentlemen, and oblige them to be explicit upon the occasion. If they
intend to continue in the service (I do not mean as volunteers for a
time of their own limiting), it is well; if not, you will be pleased to
inform them you have my orders to dismiss them the service, and
insist on their departing, and immediately, for Calcutta. Lieutenants
Padman and Clirchue have, by their answers to Mr. Strachey's
letters, sufficiently expressed their assent to the combination. You
will, therefore, order them down without delay, acquainting them
that they are no longer in the service. I shall consider of a proper
officer to send you for the command of the troops: in the mean
time, as every thing is quiet in the city, you will have no difficulty in
preserving discipline.
"Matters are very well regulated in the 1st and 3rd Brigades. The
officers of the 2d do not intend to resign till the 1st of June, by
which time Colonel Smith will have received a good supply; and I
doubt not, with the assistance of the coast, that we shall, ere long,
have the satisfaction of seeing our army in a better condition than
ever."
The following letter from Lord Clive to Mr. Verelst at Calcutta[54] is
too important to have one word omitted; for, while it shows the
nature and extent of the combination formed against all authority, it
exhibits the master mind by which the danger was foreseen and
overcome, and it explains the mode in which it was deemed best to
prevent the possibility of its recurrence:—
"Enclosed you will receive two letters, one from Mr. Martin, the other,
although not signed, I know to be Higginson's handwriting; so that
you see we are betrayed even by our own sub-secretary; and I make
no doubt but the assistant-secretary is still deeper in the plot.
"You will observe, in the last general letter, the Directors order us to
dismiss, not suspend; and I think near all the Company's servants
concerned in exciting this mutiny might not only be dismissed, but
sent home in the first ship. Such a behaviour in England would be
high treason to the state, and every man of them would be hanged.
"I hope the Council will not hesitate one moment about turning out
of the office both Stephenson and Higginson, and dismissing them
the service, if concerned in fomenting the late mutinous
combination. Indeed, very few are to be trusted; and, in my opinion,
the Council should immediately require the assistance of twelve or
fourteen junior servants from Madras and Bombay; for, I am fully
persuaded, this settlement can never be restored to order, or the
honour of the nation or the Company retrieved, until there be a total
change in the morals of individuals: and that can only be effected by
turning out the most rich and factious, and transplanting others. I
have some hopes the Directors will empower me to take such a step
in their answer by the Admiral Stevens.
"How shocked must Sulivan and those Directors be, who opposed
this appointment of field officers! Certain it is that, without their
assistance, we must have given way to the mutiny amongst the
officers; and it is equally certain, if we had, Bengal must have been
lost, or a civil war carried on to restore to the Company their lost
authority, rights, and possessions; for it is beyond a doubt, that men
capable of committing such actions as they have lately done would
soon have gone such lengths as to have made it impossible ever to
return to their native country.
"There was a committee to each brigade, sworn to secrecy; and I
have it from undoubted authority, that the officers thought
themselves so sure of carrying their point, that a motion was made
and agreed to, that the Governor and Council should be directed to
release them from their covenants. The next step would, I suppose,
have been the turning me and the Committee out of the service. In
short, I tremble with horror when I think how near the Company
were to the brink of destruction.
"The plot hath been deeply laid, and of four months' standing. I can
give a shrewd guess at the first promoters. One of them I have
already mentioned to you, who will ere long, I hope, be brought to
condign punishment.
"Remember again to act with the greatest spirit; and if the civilians
entertain the officers, dismiss them the service; and if the latter
behave with insolence, or are refractory, make them all prisoners,
and confine them in the new fort. If you have any thing to
apprehend, write me word, and I will come down instantly, and bring
with me the third brigade, whose officers and men can be depended
upon.
"I wish the Board would allow Hare two months longer to settle his
affairs: he is one of the best among the servants of Patna.
"I am, &c.

"Clive.
"P.S. A box of intercepted letters will be sent down to-morrow by
water, under a guard of Sepoys. I would advise the Board to open
every one of them."

Sir Robert Fletcher, of whose guilt, though suspected, no public


proof had as yet been adduced, wrote to Lord Clive, expressing his
willingness to pay the penalty bond into which three officers in
whom he took an interest had entered, in order to remunerate those
who might suffer in consequence of their being prominent as
advocates of the claims of their brother officers. The following
answer of Lord Clive is a valuable document, as it exhibits, in a clear
and convincing manner, the erroneous principles and the baneful
results of this part of the combination; of the total impossibility of its
being recognised as either consistent with law or honour by any
constituted authority. It shows, also, that disposition to forgive the
young and inexperienced, which his Lordship throughout these
proceedings entertained:—
"I have this morning received yours without date.[55] Your proposal
to pay the 500l. penalty for the two M'Phersons and Ensign Patton is
what I never can approve of. Were they the best officers in the
world, I would not consent to receive them again into the service on
such terms.
"The engagement entered into among themselves is not only
mutinous and absurd, but illegal; and, therefore, the penalty cannot
be recoverable by law. Besides, I consider the paying of it actually
raising a subscription ourselves for the benefit of those who do not
retract: nor is it at all unlikely that, were they to find the penalty
money paid, a collusion would follow, for many of them to retract, in
order to obtain a maintenance, nay, an independency, for the
remainder. If only fifty of them should return on that plan (and I am
sure there must be a much greater number who would be glad to
return on any terms), a fund amounting to no less than 25,000l.
would be established, which, at 4 per cent. interest, would produce
1,000l. per annum. In short, the affair will not bear a moment's
reflection; and I must insist upon your dropping all thoughts of it.
You will please to communicate my sentiments to Major Ironside,
positively forbidding him to pay the penalty for his brother.
"If the young man repents of his association, and is desirous to
resume the service, I consent to his being restored, the General and
you having no objection; but I expect that the penalty money, that
unjust debt of false honour, shall not be paid.
"Be assured that the report of my having written, or in any manner
applied for the return of the gentlemen you hint at, is without
foundation.
"Neither Lieutenant Britton, nor any of those who signalised
themselves in the combination, shall have my consent to be
restored, however strongly they may be recommended to my
protection.
"Captain Hampton and Kinloch have both resigned. The former,
together with Ensign Pellans, I have ordered from Midnapore to
Calcutta."
From Lord Clive's private correspondence with the commandants of
brigades, in the beginning of June, it appears, that the officers,
disheartened and disunited, sought only to save themselves from
that ruin and disgrace which they were now sensible they had
brought upon themselves. No victory was ever more complete than
that which he had gained; and it must have been the more
gratifying, as he owed his success almost exclusively to his personal
wisdom, firmness, and prompt decision. He met every danger, as it
arose, with an unshrinking mind. Satisfied that concessions would
only generate further demands, he made none: but when he had
vindicated authority, and restored order, he displayed a degree of
temper and of clemency worthy of his character. He attended to the
petitions of many to be restored to the service which they had too
hastily resigned. From such indulgent consideration he excluded
most of the senior officers, and all those persons whose character
made it desirable to keep them out of the service, with all those who
had been prominent as ringleaders; while officers who had not
actually resigned, however intemperate their threats and conduct
had been, were pardoned on expressing contrition for their past
conduct.
After subduing this combination in the military employed in the
provinces, Clive appears to have thought that the bad spirit which
existed at Calcutta required strong measures.
"The spirit of civil as well as military mutiny," he observes[56], "that
has lately appeared in Calcutta deserves so much of our attention,
as to mark the most turbulent, whether Company's servants, or free
merchants, and resolutely send them to Europe; for Bengal never
can be what it ought to be whilst licentiousness is suffered to
trample upon authority."
When Captain Goddard, and some others, came forward to accuse
Sir R. Fletcher of having encouraged the mutinous combination of
the officers of his brigade, Lord Clive placed him under arrest. An
appeal was made by Sir Robert to have his case judged by the
Governor in Council; but this Lord Clive, though disposed to oblige
him, declined. "Your repairing to Calcutta," his Lordship
observed[57], "in order to be tried by the President and Council,
upon an accusation your exculpation from which depends merely
upon military law, is totally unprecedented, and therefore improper
for me to comply with. That you may not, however, imagine that I
intend to take any other part upon this occasion than my public
station requires, be assured that the court-martial to be held upon
your late conduct will be assembled by an order from the Board, and
the sentence confirmed or disapproved by them."
The junior field officers, who had, by their recent conduct, entitled
themselves to the fullest approbation of Lord Clive, presumed upon
their services so far as to send a memorial, claiming the right of
sharing in the salt revenue; an allowance which, in the military line,
had been limited to their seniors.
This memorial Lord Clive prevented being delivered; pointing out, at
the same time, to the memorialists the injury they would do
themselves, and the impropriety of Government complying with so
unreasonable a request. They attended to his advice, and the
memorial was withdrawn; on which he addressed to them the
following flattering letter:—
"Colonel Smith[58] has undoubtedly acquainted you that I declined
presenting your memorial to the Board previous to my receipt of
your application for withdrawing it; and I conclude that the
arguments I urged against the memorial, in my letter to him, have
convinced you of my wish to preserve the enjoyment of the present
emoluments to the field officers upon this establishment. The
general good of the whole, added to the consideration that every
supernumerary Major will succeed, upon vacancies, to a share in the
salt trade, will, I hope, prevail upon you to rest satisfied with the
present distribution.
"I cannot omit this opportunity of mentioning how sensible I am of
the service done by you, and the other field officers, on the late
mutinous combination; as without such assistance the resolution of
the President and of the Council must have proved ineffectual. And,
perhaps, you will not be displeased upon my assuring you, that, in
my letters to the Court of Directors, I have represented your
conduct, upon that particular occasion, in the very favourable light it
so justly deserved."
For Sir Robert Barker Lord Clive had the sincerest regard[59]; but he
always regretted the too easy character of that excellent officer. It
appears by a letter from Sir Robert Barker, that some observations
which had been made by the Governor upon Lieutenant Vertue's
court-martial, of which he was President, and upon the subject of
bazars, had reached him; and that he addressed Lord Clive in a tone
of complaint. The following observations, made by the latter in reply,
are interesting, both as they exhibit that frankness with which he
ever explained himself to those whom he regarded, and the opinion
he entertained of those indirect sources of emolument which military
officers in India, who held commands, so long continued to derive
from the sale of liquor and bazars:—
"I have received your letter[60] of the 3d of August, and rejoice to
find that you have recovered your former state of health. Orders are
sent to the commanding officers to appoint a greater number of
members than thirteen, which, I hope, will prevent these delays in
future.
"I am sorry you should think yourself obliged to defend your own
conduct, as well as that of the members of the general court-martial
appointed to sit upon the trial of Lieutenant Vertue. When I
suggested to you my opinion at Bankepore, I addressed myself to
you alone, without mentioning the other members. The liberty I then
took very nearly regarded your honour and reputation, as well as the
welfare of the East India Company, in which is included the welfare
of the nation.
"I must call to your remembrance some particular expressions I
made use of that morning at breakfast, as others were present, and
can prove the truth of what I assert. I told you, that, where
conscience was in the case, exclusive of the sacredness of an oath,
the world should not bias me to swerve from my opinion; but where
that was not so, and I was convinced in my own mind, a man was
guilty, neither apprehensions of law, or any deficiency in forms,
should influence me to act in favour of those who were not
deserving of it. I told you, at the same time, all the general officers
in Great Britain would canvass this general court-martial, and that
their attention would be more particularly fixed upon you, the
President. These were my words, or words to that purpose; this also
is my opinion, which I am not ashamed to declare to the whole
world. If, therefore, any busy, intermeddling person has represented
to you my expressions in another light, he has represented a falsity.
"With regard to the bazar duties, you may be assured from me, that,
when I mentioned the circumstance of Sir Robert Fletcher's conduct,
I was an utter stranger to any duties whatever being collected by
the commanding officers on the necessaries of life. I never received
such myself, or knowingly suffered others under me to receive them,
either upon the coast or at Bengal; and had Colonel Smith, when he
prided himself upon never having received bazar duties, informed
me that he had allowed Colonel Peach to receive them, it would
have been more consistent with that sincerity which he has always
professed.
"No one has shown himself a greater friend to the field officers than
myself; yet they seem already to forget the great advantages they
enjoy. However, I must remark, that, to an officer whose pay and
emoluments amount to 12,000l. per annum, the bazar duties can
scarce be an object.
"I am surprised to find myself accused of erecting Colonel Gunge at
Patna. To speak plainly, Barker, I never established a Gunge in my
life, and never will; because I never approved of receiving duties on
the necessaries of life; although I do not think those officers much in
fault who have done the same from prescription only. Colonel Gunge
was created by Colonel Caillaud, and revived by Colonel Cook. The
Committee have forbid this custom in future.
"To conclude, the style and diction of this last letter is so contrary to
Sir Robert Barker's natural disposition, that I am persuaded some
evil-minded persons, who have their own interests more than your
reputation at heart, have been the occasion, through
misrepresentation. However, since my friendship for you is
mistrusted, and the regard and attention which I have shown for
your welfare, from the day of your embarkation to this hour,
forgotten, I can only lament your misfortune and mine, that there
should be men in the world who can make these impressions. For
my own part, I am almost weary of the burden. I have found the
pride, ambition, resentment, and self-interestedness of individuals so
incompatible with the public good, that I should have given up the
contest long ago, if I had not set the greatest value upon my own
reputation, which is all I must expect to preserve upon my return to
England, after so odious and disagreeable an undertaking."
In a second letter[61] upon the same subject, Lord Clive observes,—
"With regard to bazars, the vindication of yourself amounted so near
to an accusation of me, that I could not avoid replying as I did. You
must, however, have been by my letter convinced of your mistake in
supposing that I either established, or enjoyed any advantages from,
the Gunge at Patna; and I hope you are no less convinced that my
arguments against the practice of levying duties upon the
necessaries of life were urged with as much tenderness as the
nature of the subject would admit. I wished to prevent your doing in
future what I was of opinion would affect your reputation, but I did
not suppose that what had passed was from the motive of extortion
or avarice.
"Be assured, Barker, upon the whole, that all I said and wrote was
dictated by a sincere regard to your honour, and by a desire to see
you act with propriety and dignity in matters which I judged were of
no small importance to your own character."
The officers concerned in the combination, who were brought to a
court-martial, were all cashiered except Captain Parker, and he was
dismissed by an order of Government. That more severe sentences
were not awarded to several who were guilty of mutiny, appears to
have been occasioned by some doubts on the part of the Court by
which they were tried as to an expression in the Mutiny Act, which
subjected those to martial law who have contracted to serve the
Company; and it was conceived that the acceptance of a commission
formed no contract. This interpretation was erroneous; and had it
been otherwise, it would have been as illegal to have deprived an
officer of his commission as of his life. But we cannot be surprised
that, where a doubt was raised, a body of officers unskilled in law,
though they might have deemed it essential to maintain
subordination, were disposed to as much lenity as was compatible
with that object.
Lieutenant Stainforth, and another officer, were accused, among
other crimes, of a declared intention of assassinating the Governor.
Such assertions were certainly made, but no overt act warranted the
belief of the intention; and it was never credited by Lord Clive, who
alluded to it on his addressing the officers and men at Monghyr. He
was speaking, he said, he was assured, to Englishmen, not
assassins. To Lieutenant Stainforth, when ordered to England with
the other officers who were dismissed, his secretary addressed, by
his desire, a letter pointing out in kind but decided terms the
impossibility of compliance with his request to be restored to the
service; and that, even if he were restored, the officers, after what
had passed, would refuse to do duty with him. Towards this officer,
and others, who had shown in their language and acts the extreme
of personal hostility to him, Clive, neither at the moment nor
afterwards, cherished any resentment. His efforts to establish
discipline, and repress an insubordinate and mutinous spirit, were
strong and uncompromising; but, that object gained, he appears to
have shown as much lenity and consideration for individuals as was
compatible with the maintenance of that authority which had been
so violently assailed.
The circumstances under which Lord Clive had to act, and the
difficulties he had to overcome, are well stated by his successor, Mr.
Verelst, in a work which he subsequently published in defence of his
own conduct.
"The impolitic arrangement of affairs[62] was among the least evils
of the Company's situation, antecedent to Lord Clive's arrival. The
dissolution of government in Calcutta kept pace with that of the
country. A general contempt of superiors, a habit of equality among
all orders of men, had obliterated every idea of subjection. To
reclaim men from dissipation, to revive a general spirit of industry, to
lead the minds of all from gaudy dreams of sudden-acquired wealth
to a patient expectation of growing fortunes, were no less difficult in
execution than necessary to the existence of the Company. Large
sums of money, obtained by various means, had enabled many
gentlemen to return to Europe. This cause, superadded to the
massacre of Patna, occasioned a very quick succession in the
service, which encouraged a forward spirit of independency, and
produced a total contempt of public orders, whenever obedience
was found incompatible with private interest. To check such
impatient hopes, where youths aspired to the government of
countries at an age scarcely adequate to the management of private
affairs, four gentlemen, being called from Madras, were admitted
into Council.
"The universal discontent among the civil servants which had arisen
from the late measures, restraining the power of individuals, was
hereby greatly increased; and, united with the mutinous spirit of the
military officers, broke forth, the following year, into a flame, which
threatened destruction to the English empire in Bengal.
"This event, though among the transactions of a later period, may,
not improperly, be here explained. The military in Bengal had for
several years enjoyed an indulgence beyond those in the other
settlements of the Company, which first arose from the bounty of
the Subahdar, when they were employed in his service. By the
advice of an officer who had long commanded the Company's troops
upon the coast of Coromandel, with great reputation to himself and
honour to the nation, representing this extraordinary allowance as
destructive of discipline, the Directors, in their public letters, had
frequently ordered the double batta to be withdrawn.[63] Such
directions, in a settlement where all idea of subordination was lost,
and where the conduct of the superior servants respecting their own
interests could ill be reconciled with a rigid exaction of obedience to
the Company's commands in others, produced little effect. One
feeble effort was made; but a remonstrance from the military
induced a ready submission on the part of the Governor and Council.
The Select Committee, very justly conceiving, that a regard to
private interest would not justify a disobedience to the positive
injunction of their superiors[64], resolved to carry the measure into
immediate execution."
A historian little disposed to take a favourable view of Lord Clive's
motives or actions, commenting upon his conduct on this trying
occasion, observes[65]; "It was one of these scenes, however, in
which he was admirably calculated to act with success. Resolute and
daring, fear never turned him aside from his purpose, or deprived
him of the most collected exertions of his mind in the greatest
emergencies. To submit to the violent demands of a body of armed
men, was to resign the government."
This praise, reserved as it is, has value coming from such a quarter;
but it is fair to state, that the historian formed his judgment on this
and other acts of Lord Clive solely from public records. He has not,
like the writer of these pages, had access to that private
correspondence, by which he has been enabled to examine every
letter or note which Clive received or wrote daily to persons of all
ranks and classes; nor could he trace his feelings, as the writer has
done, at every hour of this great crisis, during which, it appears from
the most authentic documents, that his mind was not only cool and
unshaken, but that, though often ruffled with honest indignation at
proofs of cowardice, treachery, and guilt, he was never betrayed into
one act unworthy of his private or his public character. He not only
warned all of the dangers into which they were rushing headlong,
but personally entreated them to remain in the path of duty; or,
when they had left it, to return. In the prosecution of the most guilty
he never mingled his own name; on the contrary, we find him
extending to all such as had attacked him personally, as much
clemency and consideration as was compatible with the public
interests; and this kindness not only reached those who deceived
him most grossly, but was extended to an unfortunate individual
who, in a moment of rage, had threatened to become his assassin.
On the other hand, when warmth of temper and impatience led him,
as it sometimes did, to express himself with unkindness, if not
harshness, to those whose efforts and zeal did not keep pace with
his own, we find him treating their remonstrances in a manner
which, from its frankness and tone of friendship, was alike calculated
to establish his own superiority, and to gain their respect, if not their
attachment.
There is no event of his life in which Lord Clive showed more
knowledge of human nature, and a more intimate acquaintance with
all the elements which compose military bodies, than on this
occasion. While he visited with severity of punishment bold
offenders, he gave the most delicate attention to the high feelings of
honour, which had kept others free from guilty associations. When,
under an impression that there was a defect in the articles of war, he
would restore none of those who had joined the combination, until
they signed a contract to serve a certain period, he made no such
call upon those who had been true to their duty. The contract might
be necessary, he said; but men who had undergone such a trial
ought not to be insulted with a suspicion that any further tie was
required to bind their allegiance. They repaid this confidence by
voluntarily insisting upon signing the contract, into which officers
with whom they continued to serve had been compelled to enter.
Lord Clive appears, from both his public and private letters, to have
estimated the complete victory he had obtained on this occasion
beyond any he ever gained in the field: and, in fact, it was with
reason that he did so. Considering that upwards of two hundred
officers had not only combined, but had pledged themselves by
every tie that could bind men, to oppose authority, Clive had solid
ground for exultation in the success of measures, planned and
executed by himself, the result of which was to restore
subordination, to vindicate an insulted Government, and to save the
country from ruin. And this achievement was the more gratifying,
from being attended with comparatively few consequences that were
ultimately injurious to any great body of individuals. It must,
moreover, have been satisfactory to Lord Clive, to know that this
combination had not its source in any of those evil designs by which
such mutinous proceedings are often marked. It originated in the
too long continuance of a temporary grant, of an extra allowance, to
which young officers (and almost all concerned were such) soon
adapted their expenditure; and when luxuries, recommended by the
climate and character of the service, became necessaries, they were
not likely to recognise the justice of the distinction, which had been
made by the Directors, between the boon of a Nabob (which the
double batta first was), and a direct payment from the treasury of
Government, which it became after the Company had obtained the
grant of the Dewannee.
The opposition which the officers offered to the reduction of their
allowances, was in some measure countenanced by the local
Government, which had evaded the execution of the orders issued
by the Directors for the abolition of double batta. Nor is it very
surprising, that the officers should have heedlessly rushed on the
extreme measure of resigning their commissions, when we advert to
the encouragement which their combination had received from an
officer of the rank and reputation of Sir Robert Fletcher; and to the
sympathy and support which had been expressed and afforded by
the civilians, among whom were some who held confidential
situations under the Government, and others who were believed to
have great influence in England: these persons were, as well as the
military, discontented with the revisions and reductions which Lord
Clive had adopted. Under these circumstances, and under the
impression that their services could not be dispensed with, at a time
when the country was not only unsettled, but threatened with
invasion by a large Mahratta army, the officers felt confident that the
Governor must have yielded to their demands. But in thus judging,
they appear to have little understood the character of him with
whom they had to contend; and when they were met, not only with
an unyielding spirit, but treated at once as criminals, and every
measure adopted for their punishment, they fell without a struggle.
They had prepared no means to go beyond their first act; and this,
though it was by some brought forward as a proof of want of
forethought and weakness, was, in fact, a proof of their innocence of
any deliberate intention to injure the interests of their country:
though, but for the overruling genius of Clive, the most fatal injury
would assuredly have been the consequence of the success of their
guilty combination; for, had they succeeded, the civil Government
would have lost all respect, and the usurpation of the public
authority, by a combination of officers, would have given an example
to their men which, if followed, would have been alike destructive of
all discipline, and have terminated in the subversion of the English
government in Bengal.

FOOTNOTES:
[1] This account is an abstract of a narrative of the mutiny,
written by the late Sir Henry Strachey, and which forms Appendix
1st to the 9th Report of the Committee of Secresy of the House of
Commons, A.D. 1773.
[2] Vide Ibid. Appendix, No. 1. a. fo. 699.
[3] The Court's orders for this reduction, dated the 11th of June,
1764, are very positive and peremptory.
[4] The order was expected some time before it was issued.
[5] Vide 9th Rep. of Com. of Secresy, App. No. 1. fo. 699. This
remonstrance sets forth the high rates of supplies at a distance
from the coast, objections of servants to act except in one
capacity, and various expenses and hardships entailed by their
situation, and aggravated by the reduction of allowances.
[6] Ibid. No. 2. fo. 700.
[7] Vide 9th Rep. of Com. of Secresy, App. No. 4 fo. 702.
[8] Ibid. No. 5. fo. 702.
[9] ibid. No. 6. fo. 702.
[10] Vide 9th Rep. of Com. of Secresy, App. No. 7. fo. 702.
[11] Ibid. No. 8. fo. 702.
[12] Vide 9th Rep. of Com. of Secresy, App. No. 9. fo. 703.
[13] Vide 9th Rep. of Com. of Secresy, App. No. 10. fo. 703.
[14] Ibid. No. 12. fo. 734.
[15] Vide 9th Rep. of Com. of Secresy, App. No. 13. fo. 704.
[16] Ibid. No. 14. fo. 704.
[17] Vide Letter, 1st May. No. 17. fo. 705. No. 18. fo. 705.
[18] Vide 9th Rep. of Com. of Secresy, Letter 4th May, App. No.
21. fo. 706.
[19] Ibid. No. 22. fo. 706.
[20] Vide Letter 5th May, No. 23. fo. 706.
[21] Vide 9th Rep. of Com. of Secresy, Letter 5th May, App. No.
24. fo. 707.
[22] Ibid. No. 27. fo. 707.
[23] Ibid. Nos. 28. and 30. fo. 708.
[24] Vide 9th Rep. of Com. of Secresy, App. No. 29. fo. 708.
[25] Ibid. No. 31. fo. 708.
[26] Ibid. No. 32. fo. 708.
[27] Vide 9th Rep. of Com. of Secresy, App. No. 38. fo. 780.
[28] A legacy of 70,000l. was bequeathed by Meer Jaffier Aly
Khan, Nawab of Bengal, in 1765, to Lord Clive, and paid by his
Lordship, in the year 1766, into the Company's treasury at Fort
William, to run at interest at the rate of 8 per cent., as an annual
fund for the support of European officers and soldiers, who may
be disabled or decayed in the Company's service in Bengal, and
for the widows of officers and soldiers who may die on service
there, 8th June, 1766.
The Company extended this donation afterwards to the benefit of
all invalided, disabled, or superannuated officers and soldiers, and
the widows of such officers and soldiers as may die in their
service in any of their settlements in the East Indies, pursuant to
an agreement stipulated between them and Lord Clive in the year
1770, by which the former establishment of shares was altered to
the present moieties or proportions specified as follows:—
All commissioned or warrant officers shall have half the ordinary
stated pay they enjoyed while in service.
Serjeants belonging to the artillery shall receive 9d. per day, and
such as have lost a limb 1s. per day: private men of the artillery,
6d. per day; and such as lose a limb, 9d. per day.
All other non-commissioned officers and private men shall receive
4¾d. per day.—23d July, 1771.
Vide Parliamentary Papers, A.D. 1773, vol. iv. report 9. p. 535.
[29] A station adjoining the city of Patna.
[30] Captain Goddard became afterwards a very distinguished
officer. He commanded the force that Warren Hastings sent to the
relief of the settlement of Bombay in 1778.
[31] It is not unworthy of remark, that Sir Robert Fletcher, thus
cashiered by sentence of a court-martial for mutiny, was, in 1775,
appointed, by the Court of Directors, Commander-in-chief of the
army at Madras. There he headed the opposition which set aside
Lord Pigot from the government of Madras in 1776.
No mention is made in the text of John Petrie as one of the
ringleaders of the mutiny of the officers of the Bengal army in
1766.
This man was sent home by Lord Clive on that account with a
rope about his neck; but so much do things depend on the party
who may be in power, or influence, with the Court of Directors,
that this very John Petrie obtained an appointment high in the
civil service at Bengal, through the interest of his friends the
Johnstones, who were in opposition to Lord Clive's party in
England.
[32] 20th May, 1766.
[33] Letter to General Lawrence, dated Calcutta, 1st June, 1766.
[34] Letter to Sir R. Fletcher, 28th June, 1765.
[35] Mahommed Ali, at Madras.
[36] Letter to Sir R. Fletcher, under date 6th August, 1765.
[37] Letter to Mr. Sumner, dated 7th July, 1765.
[38] Letter to General Carnac under date 8th July, 1765.
[39] Letter to Mr. Verelst, dated 11th July, 1765.
[40] Vide letter from Colonel Richard Smith to Lord Clive, under
date 31st August, 1765.
[41] Vide letter to Colonel Richard Smith from Lord Clive, under
date 15th February, 1766.
[42] Letter to Sir R. Barker, under date 16th February, 1766.
[43] Dated 3d February, 1766.
[44] Letter to Mr. Verelst, dated 3d May, 1766.
[45] Letter to Mr. Verelst, 4th May, 1766.
[46] Letter to Sir R. Barker, p. 19.
[47] Letter to Colonel Smith, dated 15th May, 1766.
[48] Dated 16th May, 1766.
[49] Dated 18th May, 1766.
[50] Dated 22d May, 1766.
[51] Letters to Mr. Sumner and Mr. Sykes, dated 23d and 24th
May.
[52] Letter to H. Verelst, Esq., 25th May, 1766.
[53]
"Patna, le 27 Mai, 1766.
"Monsieur,
"J'ai eu l'honneur de votre lettre du quinzième courant. Le sujet
étant aussi intéressant pour vous, il sera, je crois, essentiel
qu'une lettre me soit addressée de votre part et de votre conseil
conjointement.
"Un nombre de nos officiers ont très-déshonorablement quitté le
service, sous un prétexte le plus injuste; à savoir, qu'ils n'ont pas
de quoi subsister; quoique notre militaire est le plus advantageux
du monde. Ils sont actuellement en chemin pour Calcutta.
Comme ils passeront par votre Colonie, j'ai jugé à propos de vous
l'annoncer; et je me persuade que vous ne donnerez point
d'accueil à des gens qui ont agi si indignement.

"Je suis, &c.,

"Clive."
[54] Letter to Mr. Verelst, 28th May, 1766. Messrs. Abzal's
gardens.
[55] Letter to Sir R. Fletcher, dated 30th May, 1766.
[56] Letter to H. Verelst, Esq., dated 6th June, 1766.
[57] Letter to Sir R. Fletcher, dated 3d July, 1766.
[58] Vide Letter to the Junior Field Officers, 6th October, 1766.
[59] Before Lord Clive left India, he wrote to Sir R. Barker,
earnestly advising him to remain till Colonel Smith returned, and
assuring him of his support to succeed that officer in the
command of the troops in Bengal.
[60] Letter to Sir R. Barker, dated 12th August, 1766.
[61] Letter to Sir R. Barker, dated 8th October, 1766.
[62] Vide Verelst's "View of the English Government in Bengal."
Lond. 1772. 4to, p. 56.
[63] A repetition of this command was among the particular
instructions to Lord Clive in 1764.
[64] Under the establishment of this double batta, a Captain's
commission produced little short of 1000l. per annum: when
reduced, it was worth from 650l. to 700l., as appeared upon the
action of Captain Parker against Lord Clive.
[65] Mill's "History of British India," vol. iii. p. 376.

CHAP. XVI.
IN the preceding two chapters, an account has been given of Lord
Clive's successful efforts in the arduous task of reforming the
abuses, and restoring order and discipline to the civil and military
services of Bengal: it remains to notice other public proceedings of
importance during his last residence in India.
No question connected with this period of his service was, at the
moment and subsequently, the subject of more comment and
discussion, both on the part of the Government at home and of
individuals, than the monopoly of the salt trade; the profits of which
he divided among the Governor, the Counsellors, and the senior civil
and military officers; deeming this indulgence, as he repeatedly
states in his official and private letters, indispensable to the integrity
and efficiency of the public service. Men in high station, he argued,
unless some ample and open allowance was given them, could never
be expected to be reconciled to a strict observance of the covenants
that prohibited presents, nor to the loss of that internal trade which
had been denounced as so ruinous and oppressive. The shares in
the profits from salt were, by his plan, divided according to the rank
and duties of the parties. The amount was known, and, though
liberal, it was limited, and, certainly, would not appear, from the
statements made of it, to be more than a fair remuneration to men
employed as those were to whom it was allotted.
Whether this mode of remunerating service was the best at the
period when it was adopted, and whether the monopoly of the salt
produced in the lower parts of Bengal, which the East India
Company found existing, and have ever since, under one shape or
another, continued, was advantageous or hurtful, on sound financial
principles, are questions which merit notice, both as connected with
the biography of Clive, and with the source of our Indian revenues.
The habits of thinking, and constitution, of the Court of Directors,
rendered them very adverse to granting adequate salaries to those
employed in high stations. These had all (including the military) the
privilege of trading; and to the exercise of this privilege many of the
abuses of the earlier times of the service have been justly attributed.
Clive appears to have made reiterated representations upon this
subject, impressing the necessity of adequate allowances, in some
shape, to the superior officers, in order to animate their zeal, and
preserve their public integrity. The narrow allowance to military
officers, and their being expected to gain by trade, he particularly
condemned. Writing to Major Stibbert[66], he observes, "I have
received your letter of the 17th inst., and am not a little surprised
that you should so soon request leave to return to Calcutta,
considering how short a time you have done duty in your brigade.
Your attention, I suspect, is too much taken up with commercial
affairs; a study very foreign from an officer, even of an inferior rank,
as it must frequently interfere with the services of a military station,
but particularly reprehensible in those to whom a share in the profits
upon salt is allotted. However, I admit that the death of your
attorney may make your presence in Calcutta necessary. You have,
therefore, my permission to leave the cantonments immediately, if
the service will permit, and Colonel Smith has no objection."
The ground on which he felt the necessity of assuring to military
officers of rank liberal allowances, in order that they might suitably
maintain their station in life, and enjoy a reward for long service;
and the necessity he saw for putting an end, on their part, to all
indefinite and indirect perquisites, and of giving to their minds a
tone that should elevate them above all sordid views, and make
them what their stations required they should be, is well stated in a
letter[67] to Sir Robert Barker:—"Colonel Smith is making a vigorous
progress in reforming the abuses that fall under his notice. The
monstrous charges and impositions of quarter-masters, surgeons,
&c., &c., require, indeed, the strictest scrutiny; and he seems
determined to go through it with great spirit and attention to the
Company's interest. Nor shall I be disappointed in the assistance I
expect from you in these matters, whilst I shall, at the same time,
have the satisfaction of knowing that you can enforce wholesome
regulations without creating disgust. The privilege of making bills,
and the long track of frauds introduced under the customary
disguise of perquisites, I wish to see entirely abolished. Every
emolument shall be fixed, plain and open: the medium shall, if
possible, be struck between extravagance and niggardly restrictions:
but economy shall take place. The allowance to field officers will be
so large as to prevent even their wishing for more; and, at the same
time, so reasonable, that I think the Company must approve of
them. A colonel's share of the salt produce will be from 5000l. to
6000l. per annum, or more[68]; lieutenant-colonel's and major's in
proportion; and as a further encouragement, I intend that all the
field officers shall be allowed sufficient to defray the expense of their
table. When all mean advantages are disclaimed and held in
contempt by gentlemen high in the service, reformation will, of
course, be with greater ease introduced among inferiors. You will do
me the justice to believe that I mean this as a general observation
only, and not as a necessary hint, either to yourself or any of the
field officers of your regiment, as I know you are all men of honour
and principle."
The reasons of expediency that led Clive to recommend that high
public officers, civil and military, should be remunerated by shares in
the profits of the salt trade, are stated in numerous letters. He
thought that an open, direct, pecuniary allowance would not willingly
be sanctioned by the Company out of any of the revenues which
flowed into their treasury, and still less from the profits of their
trade; and that, besides, such large avowed allowances would invite
an attack from the Crown on their patronage; and that the grasping
character of the administration in England would lead to a ruinous
interference in the nomination of men to India who had no
recommendation but their high birth and great interest.
It was the above considerations that compelled him to devise the
means he deemed least objectionable of adequately rewarding
service, in order to gain, by the tie of self-interest as well as honour,
those instruments without whose aid he was sensible the great
reform he had resolved to introduce could neither be complete nor
permanent. In Clive's correspondence and measures, at this period,
will be found the origin and introduction of that important principle
of a fair and honourable payment for service, suited to its nature
and the rank and responsibility of the individuals employed, which
has been generally ascribed to the more enlightened policy of a
subsequent administration. That his efforts failed, was owing to the
conduct of others, and particularly the public authorities in England,
who, in their attack upon the salt monopoly and its appropriation,
and in the condemnation of his measures, threw, for a period, a
disrepute upon all that he had done, which led to a revival of a great
proportion of the abuses he had corrected, and a disregard of the
principles he had established. As the salt monopoly and its
appropriation has been a subject of constant attack upon his
character, and continues, so far as the monopoly is concerned, to be
still one upon the Indian Government, the subject merits a cursory
notice, which is all that the limits and objects of this Memoir will
permit.[69]
We have already seen that, by the firman of the King of Delhi, the
English Company possessed the right of trading free from duties.
This privilege was granted to favour the kind of trade they then
carried on, which was confined to exports and imports by sea: and
the dustuck, or passport, of the English presidents or chiefs, was
respected by the Subahdar's officers to that extent. Under this
privilege the President favoured also the private trade of the
Company's servants or officers, which, though not strictly according
to the words of the firman, was never objected to.
As to the internal or carrying trade of the country, to engage in it
never entered into the plans of the Company or its servants, which
were confined to the valuable and profitable traffic between Europe
and India; and, had they thought of it, it is clear that it could not
have been profitably conducted by foreigners under a native
government, which had the power of enforcing justice in the
transactions between them and its own subjects.
But after the deposition of Suraj-u-Dowlah, and the elevation of
Meer Jaffier, the influence of the English in Bengal became
paramount; and, as they were all traders, some of them extended
their views, and availed themselves of their political superiority to
enter into the internal trade also; and, applying their partial freedom
from duties on foreign trade to circumstances totally different,
employed it to exempt themselves from duties even on their illegal
internal commerce; an indulgence which, of course, had never been
intended.
As long as Clive remained in Bengal, he checked these pretensions
by his characteristic firmness and spirit; but no sooner had he left
the country, than there was a general rush of the Company's
servants, and of Europeans of all classes, towards the interior trade
of the three provinces. In the foreign trade, the Company and its
officers had, indeed, the advantage of trading free of duties, but the
returns were tardy, and in some instances uncertain; whereas in the
internal trade the return was rapid and certain; and, as they most
unjustly claimed for this trade the same exemption from duties
which they had enjoyed for the articles of their foreign export trade,
it is clear that they had it in their power to undersell the native
merchant in his own market; that, to the extent of their capital, they
had all the advantages of a monopoly; and that, as their trade
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookmass.com

You might also like