100% found this document useful (3 votes)
20 views

Instant Access to JavaScript: A Beginner's Guide, Fifth Edition Pollock ebook Full Chapters

Pollock

Uploaded by

maniantwaine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
20 views

Instant Access to JavaScript: A Beginner's Guide, Fifth Edition Pollock ebook Full Chapters

Pollock

Uploaded by

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

Download Full Version ebook - Visit ebookmeta.

com

JavaScript: A Beginner's Guide, Fifth Edition


Pollock

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

OR CLICK HERE

DOWLOAD NOW

Discover More Ebook - Explore Now at ebookmeta.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

JavaScript A Beginner's Guide Fifth Edition John Pollock

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

ebookmeta.com

JavaScript Absolute Beginner s Guide 2nd Edition Kirupa


Chinnathambi

https://ebookmeta.com/product/javascript-absolute-beginner-s-
guide-2nd-edition-kirupa-chinnathambi/

ebookmeta.com

Learning Web Design A Beginner s Guide to HTML CSS


JavaScript and Web Graphics 5th Edition Jennifer Niederst
Robbins
https://ebookmeta.com/product/learning-web-design-a-beginner-s-guide-
to-html-css-javascript-and-web-graphics-5th-edition-jennifer-niederst-
robbins/
ebookmeta.com

The Complete Internet Security Manual New July 2021 Update


10th Edition Papercut Limited

https://ebookmeta.com/product/the-complete-internet-security-manual-
new-july-2021-update-10th-edition-papercut-limited/

ebookmeta.com
Being a Firewall Engineer An Operational Approach A
Comprehensive guide on firewall management operations and
best practices 2nd Edition Jithin Alex
https://ebookmeta.com/product/being-a-firewall-engineer-an-
operational-approach-a-comprehensive-guide-on-firewall-management-
operations-and-best-practices-2nd-edition-jithin-alex/
ebookmeta.com

Whereas 1st Edition Stephen Dunn

https://ebookmeta.com/product/whereas-1st-edition-stephen-dunn/

ebookmeta.com

Panic Attacks Workbook A Guided Program for Beating the


Panic Trick Violator David Carbonell

https://ebookmeta.com/product/panic-attacks-workbook-a-guided-program-
for-beating-the-panic-trick-violator-david-carbonell/

ebookmeta.com

Representation of Places Reality and Realism in City


Design Peter Bosselmann

https://ebookmeta.com/product/representation-of-places-reality-and-
realism-in-city-design-peter-bosselmann/

ebookmeta.com

Microgrids and Methods of Analysis 1st Edition Gevork B.


Garehpetian

https://ebookmeta.com/product/microgrids-and-methods-of-analysis-1st-
edition-gevork-b-garehpetian/

ebookmeta.com
Psychology Around Us Ronald Comer

https://ebookmeta.com/product/psychology-around-us-ronald-comer/

ebookmeta.com
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
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


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

2 JavaScript: A Beginner’s Guide

Key Skills & Concepts


● Using Text Editors, WYSIWYG Editors, and Web Browsers
● Defining JavaScript
● Differences Between Client-Side and Server-Side Programming

W elcome to JavaScript: A Beginner’s Guide, Fifth Edition! You’re obviously interested in


learning JavaScript, but perhaps you’re not sure what you need to know to use it. This
chapter answers some basic questions about what JavaScript is, provides a brief history of the
language, and discusses the various environments that can use JavaScript for programming.
JavaScript is ubiquitous on the World Wide Web. You can use JavaScript both to make
your Web pages more interactive, so that they react to a viewer’s actions, and to give your Web
pages some special effects (visual or otherwise). JavaScript can now even be used to perform
input/output operations or build Web servers by using Node.js!
JavaScript often gets included with Hypertext Markup Language (HTML) and Cascading
Style Sheets (CSS) as the three recommended languages for beginning Web developers
(whether you build Web sites for business or pleasure). Of course, you can build a Web page
by using only HTML and CSS, but JavaScript allows you to add additional features that a
static page of HTML can’t provide without some sort of scripting or programming help.

What You Need to Know


Before you begin learning about JavaScript, you should have (or obtain) a basic knowledge of
the following:

●● HTML and Cascading Style Sheets (CSS)


●● Text editors and Web browsers
●● The different versions of JavaScript
●● Differences in client-side and server-side programming

If you have this basic knowledge, you’ll do just fine as you work through this book. Knowing
another programming/scripting language or having previous experience with JavaScript isn’t
required. This book is a beginner’s guide to JavaScript.
If you think you don’t have enough experience in one of the aforementioned areas, a closer
look at each one may help you decide what to do.

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


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

Chapter 1: Introduction to JavaScript 3

Basic HTML and CSS Knowledge


While you don’t need to be an HTML guru, you do need to know where to place certain
elements (like the head and body elements) and how to add your own attributes. This book
will reference scripts in the head section (between the <head> and </head> tags) and the body
section (between the <body> and </body> tags).
Sometimes, you will also need to add an attribute to a tag for a script to function properly.
For example, you may need to name a form element using the id attribute, as shown in the
following code:
<input type="text" id="thename">

If you know the basics of using tags and attributes, the HTML portion shouldn’t pose any
problems in learning JavaScript.
If you don’t have a basic knowledge of HTML, you can learn it fairly quickly through
a number of media. For example, you can buy a book or look for some helpful information
on the Web. A good book is HTML: A Beginner’s Guide, Fifth Edition by Wendy Willard
(McGraw-Hill, 2013). To find information about HTML on the Web, check out developer
.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Getting_started.
Occasionally, you will need to use CSS to add or change presentation features on a Web page.
We will mainly use CSS for the purposes of dynamically changing CSS properties via
JavaScript in this book. A good place to learn CSS is developer.mozilla.org/en-US/docs/Learn/
CSS/Introduction_to_CSS/How_CSS_works.

Basic Text Editor and Web Browser Knowledge


Before jumping in and coding with JavaScript, you must be able to use a text editor or HTML
editor, and a Web browser. You’ll use these tools to code your scripts.

Text Editors
A number of text editors and HTML editors support JavaScript. If you know HTML, you’ve
probably already used an HTML editor to create your HTML files, so you might not have
to change.
However, some HTML editors have problems related to adding JavaScript code (such as
changing where the code is placed or altering the code itself when you save the file). This is
more often the case when using WYSIWYG (What You See Is What You Get) editors. It is
best to use a code editor such as Sublime or Visual Studio Code (recommended) or a plain text
editor. Some examples of text editors are Notepad, TextPad, and Simple Text.

Web Browsers
Again, if you’ve been coding in HTML, you probably won’t need to change your browser.
However, some browsers have trouble with the newer versions of JavaScript. The choice of

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


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

4 JavaScript: A Beginner’s Guide

Web browser is ultimately up to you, as long as it’s compatible with JavaScript. I recommend
the latest version of one of following browsers to test your JavaScript code:

●● Google Chrome
●● Mozilla Firefox
●● Microsoft Edge

New versions of these browsers continue to be produced. The newest versions will continue
to support more features.
To give you an idea of what some browsers look like, Figure 1-1 shows a Web page when
viewed in Chrome, and Figure 1-2 shows a Web page when viewed in Mozilla Firefox.
If you have an older browser and you can’t upgrade, a number of features (mostly discussed
later in the book) may not work in that browser. Even so, the book can still help you learn the
JavaScript language itself (especially when you’re using Node.js instead of a browser), so you
don’t need to give up if you have an older browser.

Figure 1-1 A Web page viewed in Google Chrome


 
01-ch01.indd 4 27/09/19 9:56
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0

Chapter 1: Introduction to JavaScript 5

Figure 1-2 A Web page viewed in Mozilla Firefox


 
NOTE
Even if you have one of the latest browsers, your web site viewers may not, so it
is always appropriate to understand what features may not be supported in older
browsers. This book will cover how to handle a number of these issues.

Which Version?
The version of JavaScript being used by a browser is typically associated with what version
of ECMAScript it supports. You can see what ECMAScript versions are supported by each
browser at kangax.github.io/compat-table/es6/.
ECMAScript is the international standard name and specification used for the JavaScript
language, so it’s not a new language but a standard that is set for JavaScript, JScript, and other
implementations of the language. For more on ECMAScript, see www.ecma-international.org/
publications/standards/Ecma-262.htm.
At the time of this writing, the browsers recommended earlier in this chapter should support
at least ECMAScript 7. Node.js uses the V8 JavaScript engine, and the latest version of Node.js
should also support at least ECMAScript 7.

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


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

6 JavaScript: A Beginner’s Guide

Remember, It’s Not Java


JavaScript and Java are two different languages. Java is a programming language that must be
compiled (running a program through software that converts the higher-level code to machine
language) before a program can be executed. More information on the Java language can be
found at docs.oracle.com/javase/tutorial/.

Similarities to Other Languages


JavaScript does have similarities to other programming and scripting languages. If you
have experience with Java, C++, or C, you’ll notice some similarities in the syntax, which
may help you to learn more quickly. Because it’s a scripting language, JavaScript also has
similarities to languages like PHP—which can also be run through an interpreter rather than
being compiled.
If you have programming or scripting experience in any language, it will make learning
JavaScript easier—but it isn’t required.

Client-Side and Server-Side Programming


The addition of Node.js allows JavaScript to be run on the server side in addition to its traditional
space on the client side. Learning a little about these different environments will help you to
understand the type of programming that will need to be done when working on the client side
versus working on the server side.
A client-side language is run directly through the client being used by the viewer. In the
case of client-side JavaScript, the client is typically a Web browser. Therefore, client-side
JavaScript is run directly in the Web browser and doesn’t need to handle any requests on a Web
server. The limitation is that client-side JavaScript cannot directly save information (though it
can send information to a server-side program to do so).

NOTE
Using the XMLHttpRequest object and the Fetch API allows JavaScript to send and
request data from the server. These will be covered briefly in Chapter 14.

A client-side language is useful for tasks that deal with the content of a document or that
allow information to be validated before it is sent to a server-side program or script. For instance,
JavaScript can change the content of one or more elements on a Web page when the user clicks a
link or presses a button (many other user actions can also be activated).
JavaScript can also be used to check the information entered into a form before the form
is sent to a server-side program to be processed. This information check can prevent strain
on the Web server by not allowing submissions with inaccurate or incomplete information.
Rather than the program running on the server until the information is correct, that data can
be sent to the server just once with correct information. This also benefits the user, since
client-side validation can provide feedback much more quickly to allow the user to make any
corrections.

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


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

Chapter 1: Introduction to JavaScript 7

NOTE
While client-side JavaScript is able to help validate information sent to the server,
it cannot replace server-side validation since users may have JavaScript disabled
or unavailable in the device being used (which allows them to bypass the client-
side validation). For security reasons, you should always use server-side validation,
regardless of whether or not you incorporate client-side validation.

For client-side JavaScript, the Document Object Model (DOM) is provided so that you can
access the different elements in a document. This is typically accessible within a browser for
HTML documents.
A server-side language runs on a server. For example, a server-side language can be used
to receive information from the user of a Web browser and then take an action using that
information. The server-side program can send information back to the Web browser, save it
to a database, or do any number of other things that the Web browser cannot do on its own.
However, a server-side language is likely to be limited in its ability to deal with special
features of the client that can be accessed with a client-side language (like the width of the
browser window or the contents of a form before it’s submitted to the server).
Traditionally, server-side programming was done in languages other than JavaScript,
such as PHP, Perl, Python, Ruby, Java, and a number of others. When Node.js came along, it
allowed the opportunity to use JavaScript on the server side in addition to the client side.
Depending on the needs of you or your company, you may or may not use Node.js, but this
book will cover it in order to provide a more complete introduction to the JavaScript language
and the different environments in which JavaScript can be used.

Ask the Expert


Q: You mentioned that I could use a text editor or HTML editor of my choice, but I’m
not quite sure what that means. What is a text editor and where can I find one?
A: A text editor is a program that you can use to save and edit written text. Text editors range
from simple to complex, and a number of choices are available: Notepad, WordPad, and
Simple Text, to name a few. You can also purchase and download some from the Web, like
NoteTab or TextPad.
An HTML editor is either a more complex text editor or an editor that allows you to
add code by clicking buttons or by other means—often called a What You See Is What You
Get (WYSIWYG) editor.
For the purposes of JavaScript coding, you may decide to use a more code-oriented
program that can offer features such as code highlighting, completion, debugging tools, and
more, such as Visual Studio or Sublime. I recommend one of these tools since they offer
more features to assist with programming.
(continued)

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


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

8 JavaScript: A Beginner’s Guide

Q: What exactly do I need to know about using a text editor?


A: Basically, you only need to know how to type plain text into the editor, save the file with
an .html, .css, or .js extension, and be able to open it again and edit it if necessary. Special
features aren’t needed because HTML, CSS, and JavaScript files are made up of plain text,
but the features of coding tools like Visual Studio and Sublime can be extremely helpful as
you write HTML, CSS, and JavaScript code.

Q: What do I need to know about using a browser?


A: All you absolutely need to know is how to open a local HTML file on your computer (or on
the Web) and how to reload a page. If you don’t know how to open an HTML file from your
own computer, open your browser and go to the address bar. Type in file:///C:/ and press
enter. If you are using a drive letter other than C, type that letter instead of C in the example.
The browser will display files and folders from the drive and allow you to navigate to the file
you want to open. Click an HTML file to open it. The following illustration shows how this
might look after navigating into a few folders from the C drive using Google Chrome:

Q: Where do I get those browsers you mentioned?


A: Here are links for the browsers:
●● Google Chrome www.google.com/chrome/
●● Mozilla Firefox www.mozilla.com/firefox
●● Microsoft Edge https://www.microsoft.com/en-us/windows/microsoft-edge

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


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

Chapter 1: Introduction to JavaScript 9

Beginning with JavaScript


JavaScript came about as a joint effort between Netscape Communications Corporation and Sun
Microsystems, Inc. The news release of the new language was issued on December 4, 1995,
back when Netscape Navigator 2.0 was still in its beta version. JavaScript version 1.0 became
available with the new browser. (Before its release as JavaScript, it was called LiveScript.)
JavaScript is a prototype-based interpreted language that can be used in numerous
environments. To expand on this definition, let’s look at its important parts one by one.

Prototype-Based
Prototype-based means that JavaScript is an object-oriented programming language that can use
items called objects. However, the objects are not class-based, so no distinction is made between
a class and an instance; instead, objects inherit from other objects via the prototype property.
JavaScript has made changes in ES6 to allow you to use the class keyword, but the language is
not technically class-based. You’ll learn how to work with JavaScript objects in Chapter 10. You
don’t need to understand them in detail until you know a few other features of the language.

Interpreted Language
An interpreted language doesn’t require a program to be compiled before it is run. All the
interpretation is done on-the-fly by the client. The client, such as a Web browser or the Node.js
environment, is what is being used to interpret the language.
With a compiled programming language, before you can run a program you have written,
you must compile it using a special compiler to be sure there are no syntax errors. With a
scripting language, the code is interpreted as it is loaded in the client. Thus, you can test the
results of your code more quickly. However, errors won’t be caught before the script is run and
could cause problems with the client if it can’t handle the errors well. In the case of JavaScript,
the error handling is up to the client being used by the viewer.

Numerous Environments
JavaScript can be run in numerous environments. Most commonly, the environment is a Web
browser since JavaScript has basically become the default scripting language used when
adding dynamic interactivity to a Web page.
While JavaScript is often thought of in relation to Web browsers, a number of other
environments use it for programming, such as Adobe Acrobat and Flash (based on ECMAScript).
In addition to this, Node.js allows JavaScript to be used as a server-side language or to perform
input/output operations on an operating system. This book will cover JavaScript in Node.js
and in the Web browser at a beginner level to help you get started with programming in both
environments.

Putting It All Together


To begin, you might wonder how JavaScript is run in a browser. Where should you write your
JavaScript code, and what tells the browser it is different from anything else on a Web page?
The answers are general for now, but the next chapter provides more details.

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


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

10 JavaScript: A Beginner’s Guide

JavaScript runs in the browser by being added into an existing HTML document (either
directly or by referring to an external script file). You can add special tags and commands to
the HTML code that will tell the browser that it needs to run a script. When the browser sees
these special tags, it interprets the JavaScript commands and will do what you have directed
it to do with your code. Thus, by simply editing an HTML document, you can begin using
JavaScript on your Web pages and see the results.
For example, the following code adds some JavaScript to an HTML file that writes some
text onto the Web page. Notice the addition of <script> and </script> tags. The code within
them is JavaScript.
<html> This tag tells the browser
<body> that JavaScript follows
<script> This line writes the
document.write("This writes text to the page"); text inside the quote
</script> marks on the page
</body> This line tells the browser that
</html> this is the end of the script

The next chapter looks at how to add JavaScript in an HTML file by using the <script> and
</script> HTML tags. This will be your first step on the road to becoming a JavaScript coder!

Online Resources
To find additional information online to help you with JavaScript, here are some useful resources:

●● Projects/code for this book: github.com/JohnPollock/JSABG-ED-5


●● An excellent tutorial site that includes cut-and-paste scripts: www.javascriptkit.com
●● A place where you can address questions about JavaScript to fellow coders: stackoverflow
.com/questions/tagged/javascript

Try This 1-1 Use JavaScript to Write Text


pr1_1.html This project shows you JavaScript in action by loading an HTML document in
your browser. The script writes a line of text in the browser using JavaScript.

Step by Step
1. Copy and paste the code shown here into your text editor:
<html>
<body>
<script>
document.write("This text was written with JavaScript!");
</script>
</body>
</html>

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


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

Chapter 1: Introduction to JavaScript 11

2. Save the file as pr1_1.html and open it in your Web browser. You should see a single line
of text that was written with JavaScript. (To open a file in your Web browser, go to the File
menu and look for an option that says something like Open, or Open File, and select it. You
should be able to browse for the file you want to open as you would with other programs.)

Try This Summary


In this project, you copied and pasted a section of code into a text editor and saved the file.
When you opened the saved file in your Web browser, a line of text was displayed in the
browser. This text was written in the browser window using JavaScript. You will see more
about how this type of script works in Chapter 2.


Chapter 1 Self Test
1. You must know which of the following to be able to use JavaScript in a Web browser?

A. Perl

B. C++

C. HTML

D. SGML

2. Which of the following is something you should have to use JavaScript in a Web page?

A. A Web browser

B. A C++ compiler

C. A 500GB hard drive

D. A DVD-RW drive

3. The choice of a Web browser is up to you, as long it’s compatible with __________.

A. Flash

B. VBScript

C. JavaScript

D. Windows XP

4. JavaScript and Java are the same language.

A. True

B. False

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


Discovering Diverse Content Through
Random Scribd Documents
I.

Oli sunnuntai. Myrsky, jota edellisen päivän auringonlasku oli


ennustanut, ei vielä ollut tullut, mutta aurinko ponnisteli turhaan esiin
pilviseinän läpi, ja taivaanranta oli mustana.

Täsmälleen kello yksitoista saapui paroni. Hän oli virheettömästi


puettu, kuten tavallista, ja näytti kylmältä ja tyyneltä.

»Minua käskettiin saattamaan teidän ylhäisyytenne tähän


huoneeseen», sanoi Felice osoittaen kammiota.

»Kiitos!… Kuuluuko mitään uutta, Felice?»

»Ei mitään, teidän ylhäisyytenne», sanoi Felice. Sitten hän osoitti


kipsistä kuvapatsasta, joka kohosi nurkassa olevalta jalustalta, ja
jatkoi matalalla äänellä: »Hän viipyi täällä eilisiltana muitten jälkeen
ja…»

Mutta samassa kuului naisen hameen kahinaa ulkopuolelta, mikä


keskeytti
Felicen, ja paroni sanoi äänekkäästi:
»Tietysti. Sano kreivittärelle, että käyn hänen luonaan ennenkuin
lähden pois.»

Roma astui huoneeseen kasvot synkkinä ja vakavina. Hymy, joka


tavallisesti väikkyi hänen huulillaan ja välähti hänen silmistään, oli
poissa, ja kasvojen ilme osoitti päättävää puolustautumista. Mutta
paroni huomasi heti, että tuon lujapiirteisen suun ja suoraan
katsovien silmien takana piili pelko, jota Roma koetti peittää. Paroni
tervehti Romaa luonnollisen tyynesti, suuteli hänen kättään, tarjosi
hänelle kukan napinlävestään ja vei hänet istumaan nojatuoliin selin
ikkunaan, asettui itse matalalle tuolille sen eteen ja veti veltosti pois
moitteettomat hansikkaansa.

Ei sanaakaan eilispäivästä, ei tuskan eikä moitteen katsettakaan.


Pari tavallista kohteliaisuutta vain, ja sitten hän siirtyi asiaansa.

»Kuinka pitkä aika siitä on, kun viimeksi istuin kahden kesken
sinun kanssasi! Ja kuinka täällä on muuttunut! Sinun suloinen pikku
pesäsi on kuin luostarin koppi! Minä olen totellut käskyäsi, kuten
näet, olen ollut poissa täältä koko kuukauden. Se oli vaikeata —
hyvin vaikeata — ja monta kertaa olen sanonut itselleni, että se oli
ajattelematonta. Mutta olethan sinä nainen. Sinä olit järkähtämätön,
ja minun täytyi alistua. Mutta mitä sinulla nyt on minulle
kerrottavaa?»

»Ei mitään», sanoi Roma katsoen suoraan eteensä.

»Eikö yhtään mitään?»

»Ei kerrassaan mitään.»


Roma ei liikkunut eikä kääntänyt kasvojaan, ja paroni istui hetken
ja katseli häntä. Sitten hän nousi ja alkoi astua edestakaisin
huoneessa.

»Ymmärtäkäämme toisiamme, lapseni», sanoi hän hellästi.


»Annatko minulle anteeksi, jos minun täytyy palauttaa mieleesi
tunnettuja asioita?»

Roma ei vastannut, vaan tuijotti valkeaan, kun paroni seisoi uunin


vieressä vastapäätä häntä.

»Kuukausi sitten eräs parlamentinjäsen, vastustuspuolueeseen


kuuluva politikoitsija, joka vuosikausia on vaikeuttanut hallituksen
töitä, piti kapinallisen puheen ja joutui tekemisiin lain kanssa. Siinä
puheessa hän myöskin ahdisti minua ja — täytyykö minun sanoa se
— lausui hävyttömiä juoruja sinusta. Parlamentti ei ollut koolla, ja
minä saatoin antaa vangitsemiskäskyn. Hänen osakseen olisi
oikeastaan tullut ehkä vankeus- tai maanpakolaisuusrangaistus,
mutta sinä katsoit soveliaaksi astua väliin. Sinä ehdotit
rangaistuskeinoa, joka oli viisas ja laajalle vaikuttava. Minä myönnyin
ja käskin virkamiesteni hämmästykseksi vapauttamaan vangitun.
Mutta häntä ei siitä huolimatta kuitenkaan ollut päästettävä pakoon,
Sinä otit rangaistaksesi häntä. Mitä viisaimmalla ja vaikuttavimmalla
tavalla sinun piti poistaa hänen loukkauksensa jäljet ja kostaa
hänelle hänen tekonsa. Mies oli suuri salaisuus — sinun piti päästä
tuon salaisuuden perille. Häntä epäiltiin vehkeilemisestä valtiota
vastaan — sinun piti ottaa selko hänen hankkeistaan. Kuukauden
kuluessa sinun piti antaa hänet minun käsiini ja minun piti saada
tietää hänen sielunsa syvimmät salaisuudet.»

Hieman oli vaikea pysyä tyynenä paronin puhuessa, mutta hän


pyyhki vain kiharan otsaltaan ja istui ääneti.
»Nyt on kuukausi lopussa. Minä olen antanut sinulle tilaisuuden
olla ystäväsi kanssa niin paljon kuin itse halusit ja katsoit parhaaksi.
Oletko saanut selville mitään hänestä?»

Roma vastasi rohkeasti: »En.»

»Siis ponnistuksesi ovat epäonnistuneet?»

»Täydellisesti.»

»Sinä siis luultavasti luovut aikeestasi rangaista tuota miestä siitä


syystä, että hän solvasi sinua?»

»Olen aikoja sitten luopunut siitä.»

»Omituista! Hyvin omituista! Ja hyvin onnetonta myöskin, sillä tällä


hetkellä olemme tulleet käännekohtaan, jolloin olisi kahta vertaa
tärkeämpää kuin ennen, että hallitus saisi kaikki tiedot, mitä sinä voit
hankkia. Mutta sinun suunnitelmasi oli hyvä, enkä koskaan voi olla
kyllin kiitollinen sinulle siitä. Ja vaikka sinun ponnistuksesi ovat olleet
turhat, ei sinun tarvitse hätäillä. Sinä olet antanut meille johtolangan,
joka on saattanut meidän ponnistuksemme onnistumaan, ja sinä voit
vielä rangaista tuota miestä, joka julkisesti solvasi sinua niin
pahasti.»

»Kuinka minä voin häntä rangaista?»

»Todistamalla, että Davido Rossi on eräs henkilö, joka tuomittiin


majesteettirikoksesta kuusitoista vuotta sitten.»

»Tuo on naurettavaa». sanoi Roma. »Kuusitoista kuukautta sitten


en ollut ikinä kuullut Davido Rossin nimeäkään.»
Paroni kumartui hiukan ja sanoi:

»Oletko koskaan kuullut nimeä Davido Leone?»

Roma vajosi tuoliinsa ja tuijotti suoraan eteensä.

»Tyynny; lapseni!» sanoi paroni hyväillen, meni lattian yli


katsomaan ulos ikkunasta ja taputti Romaa olkapäälle.

»Kerroinhan sinulle, että Minghelli on palannut Lontoosta?»

»Tuo petturi!» sanoi Roma ylenkatseellisesti.

»Olkoon vaan. Ihmisellä, joka uhraa elämänsä saadakseen selville


rikoksia, on useimmiten itsellään rikoksellinen sielu. Mutta sivistys
tarvitsee kadunlakaisijoitakin, ja oli hyvin onnellista, että satuit
ehdottamaan juuri tuota henkilöä. Kaikki, mitä olemme tehneet, on
sinun alkuunpanemaasi, ja kun ystäväsi lopullisesti saatetaan
oikeuden käsiin, saamme siitä kokonaan kiittää sinua.»

Uhkaava katse Roman silmissä hävisi, ja hän nousi tuskan ilme


kasvoissa.

»Miksi kiusaatte minua noin?» sanoi hän. »Sen jälkeen, mitä on


tapahtunut, ymmärtänette, että olen hänen ystävänsä enkä hänen
vihollisensa?»…

»Ehkä», sanoi paroni, ja hänen kasvonsa olivat jäykät kuin


kuolema.
»Istu ja kuuntele mitä sanon.»

Roma istui, ja paroni palasi uunin luo.


»Sanon että annoit meille johtolangan, kun ilmoitit kolme seikkaa.
Ensiksi, että Davido Rossi tunsi tohtori Rosellin elämän Lontoossa.
Toiseksi, että hän tunsi kertomuksen tohtori Rosellin tyttärestä Roma
Rosellista Kolmanneksi, että hän oli ollut jonkun aikaa viinurina
Grand Hotellissa Roomassa. Pari vähäpätöisempää seikkaa saatiin
selville sattumalta, nimittäin että hän kerran oli ollut tallirenkinä New-
Yorkissa ja että hänen äitinsä hukutti itsensä Tihenin ja että hänet
kasvatettiin löytölasten kodissa. Noiden seikkojen avulla on saatu
selville kahdeksan seikkaa. Salli minun luetella ne.»

Nojautuen uuniin ja levittäen toisen kätensä paroni laski nuo seikat


sormillaan.

»Ensimmäinen seikka. Muutamia vuosia enemmän kuin


kolmekymmentä vuotta sitten eräs vaimo kantaen lasta ilmoittautui
Rooman viranomaisille saadakseen lapsensa merkityksi kirjoihin.
Hän sanoi nimensä olevan Leonora Leone ja tahtoi että poika
merkittäisiin kirjoihin nimellä Davido Leone. Mutta virkamies sai
selville, että vaimon nimi oli Leonora Rossi ja että hänet oli vihitty
kirkollisten menojen mukaan, mutta siviilivihkiminen puuttui. Lapsi
merkittiin siis kirjoihin Davido Rossina, Leonora Rossin poikana, isä
tuntematon.»

»Häpeällistä!» huusi Roma. »Kuinka äärettömän häpeällistä!»

»Toinen seikka», jatkoi paroni muuttamatta ääntään. »Eräänä


iltana hiukan myöhemmin löydettiin erään hukkuneen naisen ruumis
Tiberistä ja hukuttautunut tunnettiin Leonora Rossiksi sekä haudattiin
Campo Veranon köyhäin hautausmaalle tällä nimellä. Samana yönä
tuntematon henkilö oli laskenut lapsen Santo Spiriton lastenseimeen,
ja mukaan oli liitetty paperi, jossa ilmoitettiin sen kastamisesta ja
nimestä. Lapsen nimeksi ilmoitettiin Davido Leone.»
Paroni löi kolmanteen sormeensa ja sanoi:

»Kolmas seikka. Neljätoista vuotta myöhemmin eräs


neljätoistavuotinen Davido Leone niminen poika asui erään
italialaisen maanpakolaisen kodissa Lontoossa. Tuo
maanpakolainen oli roomalainen prinssi, joka nimitti itseään tohtori
Roselliksi. Hänen perheeseensä kuului vaimo ja nelivuotias tytär
nimeltä Roma. Tohtori Roselli oli ottanut Davido Leonen
ottopojakseen löydettyään hänet kadulta.»

Roma peitti kasvonsa käsillään.

»Neljäs seikka. Neljä vuotta myöhemmin saatiin Milanossa selville


salaliitto, jonka tarkoituksena oli surmata Italian kuningas. Tuon
salaliiton päämies oli onnetonta kyllä tuo Englannissa asustava
maanpakolainen, joka oli tunnettu nimellä tohtori Roselli. Erään
sukulaisen ystävällisestä toimesta hänet lähetettiin maanpakoon
tavalla, jota kaikki valtiot käyttävät salaisissa ja vaarallisissa
tapauksissa, sillä tuo sukulainen tahtoi säästää suvun kunniaa.
Mutta tohtori Rosellin tovereille ja kirjeenvaihtajille ei osoitettu niin
suurta hienotunteisuutta, ja yksi niistä, joka vielä oli Lontoossa,
tuomittiin majesteettirikoksesta kuolemaan piiritystilan aikana
toimivassa sotaoikeudessa. Tämän toverin nimi oli Davido Leone.»

Roman jalka polki lattiaa, mutta paroni jatkoi kylmällä, tyynellä


tavallaan.

»Viides seikka. Meidän sopimuksemme Englannin kanssa sisälsi


valtiollisten rikoksellisten luovuttamisen, mutta Davido Rossi läksi
pois Englannista. Hän muutti Amerikkaan. Siellä hän oli ensin
raitiotieosakeyhtiön palveluksessa New-Yorkissa ja asui tämän
kaupungin italialaisessa osassa, mutta sitten hän kohosi
köyhyydestään ja tuli sanomalehtimieheksi. Sanomalehtimiehenä
hän herätti huomiota uudella valtiollisella ja uskonnollisella
ohjelmallaan. Kristus on sekä kansojen että yksityisten laki, ja
maailma on parannettava isämeidän rukoukselle perustetulla
hallinnolla. Tuo uskonto oli kyllin sentimentaalinen voittaakseen
intoilijain suosion tuossa lukemattomien uskontojen maassa, mutta
se koski järjestyksen, omistusoikeuden ja isänmaallisuudenkin juuriin
ja saattoi käytännössä helposti johtaa kapinaan.»

Paroni väänteli viiksiään ja sanoi hymyillen: »Davido Leone hävisi


New-Yorkista. Siitä ajasta saakka ei ole löydetty jälkeäkään hänestä.
Hän oli lakannut olemasta. Davido Leone oli kuollut.»

Roma oli ottanut kädet kasvoiltaan ja hän hypisteli puseronsa


nappeja hermostunein sormin.

»Kuudes seikka», jatkoi paroni lyöden toisen käden peukaloa.


»Kaksikymmentä viisi tai kuusi vuotta Davido Rossin syntymän
jälkeen Roomassa saapui luultavasti noin viidenkolmatta-ikäinen
mies, nimeltä Davido Rossi, Amerikasta Englantiin. Hän läksi
leipuripuotiin Sohoon kysymään Roma Rosellia, tohtori Rosellin
tytärtä, joka oli jäänyt Lontooseen, kun maanpakolainen palasi
Italiaan. Hänelle kerrottiin, että Roma Roselli oli kuollut ja haudattu.»

Roman kasvot, jotka tähän saakka olivat olleet kalpeat,


punastuivat nyt, ja hänen jalkansa polki lujemmin lattiaan.

»Seitsemäs seikka. Davido Rossi ilmaantui Roomaan, oli ensin


viinurina
Grand Hotellissa, mutta kohta sen jälkeen sanomalehtimiehenä ja
julkisena luennoitsijana, ajoi juuri samaa ohjelmaa kuin Davido
Leone
New-Yorkissa ja herätti samaa huomiota.»

»Entä sitten?» sanoi Roma »Davido Leone oli Davido Leone ja


Davido
Rossi on Davido Rossi — siitä ei maksa vaivaa puhua sen
enempää.»

Paroni pusersi käsiään niin että sormet naksahtivat ja sanoi hiukan


kiihkeämmin:

»Kahdeksas seikka. Samaan aikaan eräs mies kävi Campo


Santon virastossa kysymässä, missä Leonora Leonen hauta olisi
löydettävissä, saman naisen, joka oli heittäytynyt Tiberiin
kuusikolmatta vuotta sitten. Köyhien haudat oli sillä välin kaivettu yhä
uudelleen, mutta viranomaiset ilmoittivat, mihin paikkaan hänet
kerran oli haudattu. Hän kustansi tuon paikan erottamisen muista
haudoista ja lankesi polvilleen sinne. Tuntikausia kului, ja hän oli yhä
vielä polvillaan. Vihdoin tuli yö, ja vahdin täytyi käskeä hänet pois.»

Roman jalka oli lakannut polkemasta ja hän nousi tuoliltaan.

»Se mies», lisäsi paroni, »ainoa ihmisolento, joka ikinä oli


ajatellutkaan hakea tuon kurjan itsemurhaajan Leonora Rossin
hautaa, oli Davido Rossi. Kuka on siis Davido Leone? — Davido
Rossi. Kuka on Davido Rossi? — Davido Leone! Ympyrän kehä on
sulkeutunut, asia on selvä.»

»Oh! Oh! Oh!»

Roma oli noussut ylös ja käveli kiivaasti. Hänen huulensa olivat


kokoonpuristuneet, hänen silmänsä säkenöivät ja hän puhkesi
sanoihin, jotka tulvivat hänen huuliltaan.

»Tuo vasta on konnamaisuutta! On kyllä totta, mitä sanoitte!


Semmoisella miehellä, joka uhraa elämänsä rikoksia etsiessä, on
itsellään rikoksellinen sielu! Hänellä ei ole omaatuntoa, ei
inhimillisyyttä, ei armoa, ei sääliä. Ja kun hän on ajanut takaa
ihmistä tämän äidin haudalle asti — äidin haudalle — hän voi syödä
päivällistä ja nauraa ja mennä teatteriin! Oh, en voi sietää teitä!
Inhoan teitä! Nyt sen kuulitte! Tehkää nyt mitä tahdotte!»

Kuolonjäykkyys paronin kasvoissa muuttui kiivaan tuskan ilmeeksi,


mutta hän pyyhki kerran kädellä otsaansa ja jatkoi hetken vaitiolon
perästä:

»Lapseni, sinä et ainoastaan loukkaa minua, sinä loukkaat


oikeuden periaatetta. Oikeudella ei ole mitään tekemistä säälin
kanssa. Oikeuden sanakirjassa on ainoastaan yksi sana —
velvollisuus. Velvollisuus käski minua nimittämään tuota miestä
oikealla nimellä, jotta hänen vastustuksensa, juorunsa ja paha
vaikutuksensa loppuisivat. Ja nyt oikeus vaatii sinua tekemään
samoin.»

Paroni nojautui uuniin ja puhui tyynellä äänellä, mutta Roma jatkoi


kävelyään.

»Koska Davido Rossi on parlamentinjäsen ja parlamentti juuri on


toiminnassa, voidaan hänet vangita ainoastaan parlamentin
käskystä. Sen saamiseksi on välttämätöntä, että
kenraaliprokuraattori ilmoittaa asian hallitukselle, hallitus
puhemiehelle, puhemies valiokunnalle ja valiokunta parlamentille.
Tuota ilmoitusta varten poliisi on jo koonnut tärkeitä
todistuskappaleita, koko sarja tosiseikkoja on otettu talteen. Mutta
vielä puuttuu positiivinen todistus, ja sitä ennen kenraaliprokuraattori
ei voi tehdä mitään. Se on arkin avain, keskipiste, jonka puutteessa
kaikki muut seikat menettävät merkityksensä. Jonkun täytyy voida
vaadittaessa vaikka vannoa tunteneensa sekä Davido Leone että
Davido Rossi ja voida vakuuttaa, että nuo kaksi ovat sama henkilö.»

»Entä sitten?»

Paroni, joka oli seisahtunut, jatkoi tyynellä äänellä: »Rakas


Romaseni, täytyykö minun jatkaa? Vaikka ministereillä ei olekaan
mitään tunteita, tahtoisin kuitenkin säästää sinua. En tunne muuta
kuin yhden henkilön, joka voi antaa meille tuon puuttuvan renkaan.
Se henkilö olet sinä itse.»

Roman silmät olivat punaiset vihasta ja pelosta, mutta hän koetti


naurulla peittää pelkoaan.

»Kuinka tuhma te olette kaikesta huolimatta!» sanoi hän, »Roma


Roselli tunsi Davido Leonen, ja Roma Roselli on kuollut ja haudattu.
Oh, kyllä minä tunnen tuon jutun. Te keksitte sen itse, ja nyt se
kaivaa kuopan teille!»

»Rakas Roma», sanoi paroni jyrkästi, »jos minä tein jotain siinä
asiassa, tein sen sinun hyväksesi, mutta olkoonpa sen laita kuinka
tahansa, se ei enää minua huolestuta. Roma Roselli ei ole kuollut, ja
minun on varsin helppo tuottaa henkilöitä Englannista todistamaan
sitä.»

»Te ette uskalla! Ette uskalla! Heidät tuomittaisiin rikoksen


yrityksestä.»

»Englannissa, mutta ei Italiassa.»


Roman katse laskeutui, ja paroni alkoi puhua lempeästi:

»Lapseni, älä koeta taistella minun kanssani. On niin kiusallista


saattaa sinut vaikenemaan… On ehkä luonnollista, että tunnet
myötätuntoisuutta heikompaa puolta kohtaan. Se on kaikkien naisten
suloinen ja viehättävä, mutta epäjohdonmukainen tapa. Sinun ei
tarvitse pelätä, että Davido Rossi mestataan, vaikka hänet
vangitaankin. Hänen täytyy kärsiä uusi tutkimus, häntä puolustetaan
ja hänen tuomionsa muutetaan vankeudeksi tai pyyhitään pois
kokonaan. Siinä kaikki.»

»Ja te tahdotte minua auttamaan teitä tuossa?»

»Niin.»

»En tahdo!»

»Siis voisit, jos tahtoisit?»

»En voi!»

»Ensimmäinen vastauksesi oli oikeampi, lapseni.»

»No olkoon niin sitten! En tahdo! En tahdo! Ettekö häpeä pyytää


minulta sellaista? Oman kertomuksenne mukaan Davido Leone oli
isäni ystävä, ja kuitenkin tahdotte, että minä antaisin hänet oikeuden
käsiin, joka tuomitsisi hänet ehkä elinkautiseen vankeuteen ja
ainakin poistaisi hänet parlamentista. Luuletteko, että voisin pettää
noin kurjasti? Arvosteletteko kaikkia itsenne mukaan?… Ah, kyllä
minä tunnen sen jutun! Hävetkää!»

Paroni oli hetken ääneti ja sanoi sitten tyynesti:


»En tahdo keskustella nyt tuosta asiasta kanssasi, lapseni — olet
liian kiihtynyt etkä tiedä, mitä sanot. Tahdon vain huomauttaa sinulle,
että vaikka Davido Leone olikin isäsi ystävä, on Davido Rossi sinun
vihollisesi.»

»Entä sitten? Sehän on minun oma asiani. Jos minä annan


hänelle anteeksi, niin mitä se muita liikuttaa? Ja minä annan hänelle
anteeksi! Kenenkä muun asia se siis on kuin minun?»

»Rakas Romaseni, se on hiukan minunkin asiani, ja solvaus oli


aiottu sinun kauttasi minulle. Mutta en tahdo nyt puhua itsestäni…
Tuo seikka, että olet kokonaan muuttanut mielesi — kuukausi sitten
aioit… kuinka sanoisin… pettää tuon miehen ja nyt koetat pelastaa
hänet — se on selitettävissä ainoastaan yhdellä tavalla …. että
sinä… että sinä…»

»Että minä rakastan häntä — niin on laita», sanoi Roma ylpeästi,


mutta punastuen samassa ja vavisten pelosta.

Nyt seurasi kuolon hiljaisuus. Molempien pää vaipui alas, ja


äänetön veistokuva näytti katselevan heitä. Paronin posket
värähtivät huomattavasti, ja hän sanoi matalalla, käheällä äänellä:

»Olen pahoillani! Hyvin pahoillani! Sillä pelkään, että minun täytyy


näyttää toteen mitä sanoit, että nimittäin ministeri ei tunne ihmisyyttä
eikä sääliä. Minun täytyy ehkä ruveta näyttelemään aviomiehen osaa
kahden rakastuneen surullisessa tarinassa. Ellei Davido Rossia voi
vangita parlamentin suostumuksella, on hänet vangittava, kun
parlamenttikausi on päättynyt, ja silloin hänen asiansa on
todistettava julkisessa oikeudessa. Siinä tapauksessa sinun on
pakko esiintyä, ja vaikka olet kieltäytynyt yksityisesti todistamasta,
täytyy sinun sitten todistaa julkisesti.»
»Siihen ette voi minua pakottaa!» huusi Roma kiihkeästi, aivan
kuin hän olisi keksinyt keinon.

»Miksikä en?»

»Se on minun oma salaisuuteni. Mutta sitä ette voi tehdä!» huusi
hän.

Paroni katsoi häneen aivan kuin saadakseen selville mitä hän


tarkoitti ja sanoi sitten:

»Se saadaan nähdä.»

Samassa kuului kreivittären ärtyisä ääni, joka kutsui paronia


viereiseen huoneeseen.
II.

Roma meni makuuhuoneeseensa, kun paroni jätti hänet, ja viipyi


siellä myöhään iltaan. Huolimatta rohkeasta käytöksestään hän
vapisi pelosta ja katumuksesta. Hän ei koskaan ennen ollut
ymmärtänyt Davido Rossin vaaraa niin hirvittävän selvään ja
käsittänyt omaa suhdettaan häneen niin välittömästi.

Oliko hänen velvollisuutensa tunnustaa Davido Rossille, että hän


oli heidän tuttavuutensa alussa aikonut pettää hänet? Siten vain hän
saattoi olla uskollinen rakkaudelleen ja ansaita hänen
luottamuksensa.

Mutta miksi hänen pitäisi tunnustaa? Tuo inhottava ajatus oli


kadonnut. Jotain suloista ja hellää oli tullut sen sijaan.
Tunnustaminen nyt olisi julmaa. Se järkyttäisi Davidon kaunista
luottamusta Romaan.

Mutta ne siemenet, joita Roma oli kylvänyt, alkoivat itää. Ne


saattoivat puhjeta esiin missä tahansa ja tukehduttaa elämän, joka
oli Romalle kalliimpi kuin hänen omansa. Jumalan kiitos, että vielä oli
mahdotonta vahingoittaa Davido Rossia ilman hänen, Roman, apua.
Mutta he saattoivat murtaa hänen tahtonsa ja pakottaa hänet
auttamaan heitä, ellei laki voinut suojata häntä. Jos hän oli
naimisissa Davido Rossin kanssa, ei mikään laki Itäliassa voinut
pakottaa häntä todistamaan miestään vastaan.

Mutta jos Rossi epäröisi jostakin syystä, jos hän viivyttäisi heidän
avioliittoaan, jos hän vastaisi kieltävästi tuohon kirjeeseen, jossa
Roma oli luopunut kaikesta turhasta häveliäisyydestä ja pyytänyt
häntä kiirehtimään heidän liittoaan — mitä sitten? Kuinka hän saattoi
selittää, mikä vaara Davido Rossia uhkasi? Kuinka hän voisi sanoa,
että Davido Rossin täytyy ottaa hänet vaimokseen ennenkuin
parlamentti hajaantuu, sillä muutoin käytettäisiin häntä, Romaa,
välikappaleena, jolla Davido Rossi erotettaisiin parlamentin
jäsenyydestä ja ehkä syöstäisiin vankilaankin? Kuinka hän saattaisi
sanoa: »Minä olin Delila. Minä läksin sinua pettämään, ja ellet ota
minua vaimoksesi, en voi sinua pelastaa!»

Iltapäivä oli kulunut pitkälle, Roma ei ollut syönyt mitään aamusta


lähtien ja nyt hän makasi kasvot pielusta vasten vuoteellaan, kun
äkkiä kuului koputus ovelle.

»Ateljeessa työskentelevä mies pyytää saada tavata teitä», sanoi


Felice.

Siellä oli Bruno sunnuntaipuvussa ja pikku Giuseppe, joka tänään


oli enemmän kuin ennen pienen jalopeuran poikasen näköinen.

»Kirje häneltä, neiti», sanoi Bruno.

Se oli Rossilta. Roma otti sen muistamatta edes tervehtiä ja meni


makuuhuoneeseensa. Mutta kun hän palasi hetken kuluttua, olivat
hänen kasvonsa muuttuneet. Pilvet olivat haihtuneet ja entinen
iloisuus palannut. Vilkas, iloinen ilme loisti niistä, kun hän kiirehti
vastaanottohuoneeseen, ja ilma tuntui olevan täynnä raikkautta ja
onnea.

»Hyvä on», sanoi hän. »Sanokaa herra Rossille, että odotan häntä
kohta… tai ei pidä sanoa sitä… sanokaa, että koska hänellä on niin
kovin paljon työtä, ei hänen pidä pitää sitä välttämättömänä… Oh,
sanokaa aivan mitä tahdotte», sanoi hän, ja valkoiset hampaat ja
säteilevät silmät hymyilivät.

Bruno, joka ei ollenkaan ollut kohottanut nyrpeätä naamaansa ja


pörröistä päätänsä, sanoi:

»Hänellä on kyllä paljon puuhaa nyt, kun tuo suuri kokous on


keskiviikkona, jolloin hänen huoneensa portaat ovat yhtä täynnä
väkeä kuin Santa Scala.»

»Vai on pikku Giuseppe vihdoinkin tuotu minua tervehtimään»,


sanoi Roma.

»Hän on kiusannut minua tuomaan hänet tänne siitä saakka, kun


lupasitte, että hän saisi ruveta teidän ovenvartijaksenne kerran.»

»Juuri niin. Herrojen pitää käydä tervehtimässä neitosia, eikö niin,


Giuseppe?»

Ja poika, joka oli piilottanut pyöreän päänsä isänsä taakse, pisti


esiin kasvonsa, jotka loistivat ilosta.

»Kuulepas», sanoi Roma iskien silmää. Soittokunta kulki juuri


piazzan yli mennessään pois Pincion puutarhasta. »Mennäänpäs
katsomaan niitä», jatkoi Roma ja ottaen kiinni Giuseppen kädestä vei
hän hänet kammioonsa ja asetti hänet seisomaan kirjoituspöydälle
ikkunan eteen.
»Se on kuninkaallinen marssi, eikö niin, Giuseppe? Tunnethan
sinä kuninkaallisen marssin? Tietysti. Ja katsopas nyt kansaa ja
pappeja ja munkkeja ja ylioppilaita ja ajoneuvoja ja koiria ja
moottorivaunuja ja lapsia ja lastenhoitajia ja pikku tyttöjä ja poikia.
Se on komeata! Mutta katsopas, tunnetko sinä kuka tuo on — tuo
herra tuossa veistokuvassa?»

»Setä Davido», sanoi poika.

»Mikä viisas pikku poika sinä olet, Giuseppe.»

»Ei tarvita paljonkaan viisautta tuon tuntemiseen», sanoi Bruno


ovelta. »Se on mainio! Ihmeellinen! Ja se sulkee suun niiltä
kirotuilta… anteeksi, neiti, anteeksi.»

»Ja Giuseppe aikoo yhä vielä ovenvartijaksi?»

»Vallan vakavasti. Hän vakuuttaa, ettei hän vaihtaisi sitä virkaa


kuninkaankaan virkaan.»

»Se on oikein! Mennäänpäs nyt katsomaan emmekö löytäisi


mitään tälle tulevalle ovenvartijalle! Tulepas tänne, Giuseppe.
Autapas nyt minua vetämään auki tuo laatikko. Yksi — kaksi —
kolme! Siinä se on!»

Laatikossa oli suuri pahvikotelo, ja Giuseppen silmät säikkyivät


kuin mustat helmet.

»Mitähän tuossa kotelossa lie, Giuseppe? Voitko arvata? — No,


kuulepas nyt. Mistä pitäisit eniten, ovenvartijan hatusta, takista vai
kultatupsuisesta sauvasta?»
Pojan kasvot, jotka olivat tähän asti loistaneet, synkistyivät ja
kirkastuivat vuoroon, kun tuo vaikea vaali oli tehtävä. Vihdoin hän
katsoi Brunoon apua hakien.

»Valitse nyt — mistä?»

Mutta Giuseppe hiipi isänsä luo ja kuiskasi jotain, jota Roma ei


voinut kuulla.

»Mitä hän sanoo?»

»Hän sanoo, että keskiviikkona on hänen syntymäpäivänsä»,


sanoi Bruno.

»Hyvänen aika! Silloin hänen pitää saada ne kaikki», sanoi Roma,


ja nyt alkoivat Giuseppen jalatkin tanssia.

Nauhat katkaistiin, kotelo avattiin, ja sieltä vedettiin esille komea


hattu ja takki ja sauva, ja sitten puettiin koko komeus Giuseppen ylle.
Kesken kaikkea Roman musta koira tuli haukkuen sisään, ja kun
Giuseppe marssi kammiosta vastaanottohuoneeseen, hyppeli koira
haukkuen hänen jälessään.

»Rakas pikku poika!» sanoi Roma katsellen lasta, mutta Bruno,


joka istui pää kumarassa, vastasi vain huoahtaen.

Roma katsoi häneen ja huomasi nyt, että Brunon yksinkertaiset


kasvot näyttivät hyvin huolestuneilta. Niiden ilme oli melkein
naurettavan juhlallinen, ja hänen silmänsä olivat kosteat ja himmeät.

»Mitä nyt, Bruno?» kysyi Roma.

Bruno pyyhkäisi silmiään takkinsa hihalla ja sanoi kiihkeästi:


»Mitäkö nyt? Petosta — petosta, joka turmelee hyvän naisen
maineen — siinä kaikki. Eräs mies, joka on vuosikausia syönyt
teidän leipäänne, on valehdellut teistä, ja hän on konna ja kirottu ilkiö
ja minä tahtoisin potkaista hänet ulos tästä talosta.»

»Kuka tuota kaikkea on tehnyt, Bruno?»

»Minä itse! Minä juuri kerroin herra Rossille kaikki ne juorut, jotka
saattoivat hänet puhumaan teitä vastaan silloin paavin juhlapäivänä,
ja kun te pyysitte häntä tulemaan tänne, varoitin häntä ja sanoin, että
te aiotte kostaa hänelle nyt.»

»Niinkö sanoit, Bruno?»

»Niin juuri.»

»Ja mitä herra Rossi sanoi?»

»Mitäkö sanoi? Kumma ettei hän tappanut minua. 'Hän on hyvä


nainen', sanoi hän, 'ja jos olen koskaan sanonut muuta, otan kaikki
sanani takaisin ja häpeän'.»

»Niinkö hän sanoi?»

»Niin. Mutta piru oli minussa enkä minä uskonut häntä. Vielä eilen
pyysin häntä pysymään poissa teidän vastaanotostanne, sillä minä
olin nähnyt teidän veistokuvanne sinä aamuna, jona aloititte sen, ja
se oli irvikuva hänestä ja tarkoitti Juudasta.»

»Ja mitä herra Rossi sanoi siihen?»

»'Bruno', sanoi hän, 'jos Donna Roma olisi käyttänyt minua


vaikkapa itse paholaisen mallina, luottaisin häneen sittenkin.' Ja nyt

You might also like