100% found this document useful (7 votes)
23 views

[FREE PDF sample] JavaScript: A Beginner's Guide, Fifth Edition Pollock - eBook PDF ebooks

Fifth

Uploaded by

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

[FREE PDF sample] JavaScript: A Beginner's Guide, Fifth Edition Pollock - eBook PDF ebooks

Fifth

Uploaded by

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

Visit https://ebookluna.

com to download the full version and


explore more ebooks

JavaScript: A Beginner's Guide, Fifth Edition


Pollock - eBook PDF

_____ Click the link below to download _____


https://ebookluna.com/download/javascript-a-beginners-
guide-fifth-edition-ebook-pdf/

Explore and download more ebooks at ebookluna.com


Here are some recommended products that might interest you.
You can download now and explore!

(eBook PDF) Chemical Dependency Counseling: A Practical


Guide Fifth Edition

https://ebookluna.com/product/ebook-pdf-chemical-dependency-
counseling-a-practical-guide-fifth-edition/

ebookluna.com

(eBook PDF) Learning Web Design: A Beginner's Guide to


HTML, CSS, JavaScript, and Web Graphics 5th Edition

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

ebookluna.com

(eBook PDF) Communicating for Results A Canadian Student's


Guide 5th Fifth Edition

https://ebookluna.com/product/ebook-pdf-communicating-for-results-a-
canadian-students-guide-5th-fifth-edition/

ebookluna.com

(eBook PDF) PMP Project Management Professional Study


Guide, Fifth Edition

https://ebookluna.com/product/ebook-pdf-pmp-project-management-
professional-study-guide-fifth-edition/

ebookluna.com
(eBook PDF) The Norton Field Guide to Writing (Fifth
Edition) 5th Edition

https://ebookluna.com/product/ebook-pdf-the-norton-field-guide-to-
writing-fifth-edition-5th-edition/

ebookluna.com

(Original PDF) New Perspectives on HTML5, CSS3, and


JavaScript 6th Edition

https://ebookluna.com/product/original-pdf-new-perspectives-on-
html5-css3-and-javascript-6th-edition/

ebookluna.com

Joint Structure and Function: A Comprehensive Analysis


Fifth Edition

https://ebookluna.com/product/joint-structure-and-function-a-
comprehensive-analysis-fifth-edition/

ebookluna.com

The entrepreneur's guide to law and strategy Fifth Edition


Craig E. Dauchy - eBook PDF

https://ebookluna.com/download/the-entrepreneurs-guide-to-law-and-
strategy-ebook-pdf/

ebookluna.com

Mike Meyers’ CompTIA A+ Guide the Managing and


Troubleshooting PCs. Fifth Edition (Exams 220-901 &
220-902) Mike Meyers - eBook PDF
https://ebookluna.com/download/mike-meyers-comptia-a-guide-the-
managing-and-troubleshooting-pcs-fifth-edition-
exams-220-901-220-902-ebook-pdf/
ebookluna.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
Visit https://ebookluna.com
now to explore a diverse
collection of ebooks available
in formats like PDF, EPUB, and
MOBI, compatible with all
devices. Don’t miss the chance
to enjoy exciting offers and
quickly download high-quality
materials in just a few simple
steps!
BeginNew-Tight / JavaScript: A Beginner’s Guide, Fifth Edition / Pollock / 768-0 / Front Matter

x JavaScript: A Beginner’s Guide

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

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

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

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

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

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

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

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

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

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

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

Contents xi

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

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

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

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

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

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

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

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

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

xii JavaScript: A Beginner’s Guide

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

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

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

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

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

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

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

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

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

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

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

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

Contents xiii

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

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

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

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

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

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

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

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

xiv JavaScript: A Beginner’s Guide

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

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

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

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

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

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

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

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

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

Contents xv

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

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

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

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

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

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

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

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

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

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

xvi

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


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

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

xvii

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


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

xviii JavaScript: A Beginner’s Guide

What This Book Covers


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

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

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

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


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

Chapter 1
Introduction to JavaScript

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


Visit https://ebookluna.com
now to explore a diverse
collection of ebooks available
in formats like PDF, EPUB, and
MOBI, compatible with all
devices. Don’t miss the chance
to enjoy exciting offers and
quickly download high-quality
materials in just a few simple
steps!
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


Visit https://ebookluna.com
now to explore a diverse
collection of ebooks available
in formats like PDF, EPUB, and
MOBI, compatible with all
devices. Don’t miss the chance
to enjoy exciting offers and
quickly download high-quality
materials in just a few simple
steps!
Other documents randomly have
different content
would say farewell to her: like that, with the inflexion of a verb. As,
just occasionally, using the word "we"—and perhaps without
intention—he had let her know that he loved her.
Mr. Jegg drifted across from the window: Mrs. Haviland was already
at the door.
"We'll leave you to have your war talk out," Mr. Jegg said. He added:
"For myself, I believe it's one's sole duty to preserve the beauty of
things that's preservable. I can't help saying that."
She was alone with Tietjens and the quiet day. She said to herself:
"Now he must take me in his arms. He must. He must!" The deepest
of her instincts came to the surface, from beneath layers of thought
hardly known to her. She could feel his arms round her: she had in
her nostrils the peculiar scent of his hair—like the scent of the skin
of an apple, but very faint. "You must! You must!" she said to
herself. There came back to her overpoweringly the memory of their
drive together and the moment, the overwhelming moment, when,
climbing out of the white fog into the blinding air, she had felt the
impulse of his whole body towards her and the impulse of her whole
body towards him. A sudden lapse: like the momentary dream when
you fall. . . . She saw the white disk of the sun over the silver mist
and behind them was the long, warm night. . . .
Tietjens sat, huddled rather together, dejectedly, the firelight playing
on the silver places of his hair. It had grown nearly dark outside:
they had a sense of the large room that, almost week by week, had
grown, for its gleams of gilding and hand-polished dark woods, more
like the great dining-room at the Duchemins. He got down from the
fire-seat with a weary movement, as if the fire-seat had been very
high. He said, with a little bitterness, but as if with more fatigue:
"Well, I've got the business of telling Macmaster that I'm leaving the
office. That, too, won't be an agreeable affair! Not that what poor
Vinnie thinks matters." He added: "It's queer, dear . . ." In the
tumult of her emotions she was almost certain that he had said
"dear." . . . "Not three hours ago my wife used to me almost the
exact words you have just used. Almost the exact words. She talked
of her inability to sleep at night for thinking of immense spaces full
of pain that was worse at night. . . . And she, too, said that she
could not respect me. . . ."
She sprang up.
"Oh," she said, "she didn't mean it. I didn't mean it. Almost every
man who is a man must do as you are doing. But don't you see it's a
desperate attempt to get you to stay: an attempt on moral lines?
How can we leave any stone unturned that could keep us from
losing our men?" She added, and it was another stone that she
didn't leave unturned: "Besides, how can you reconcile it with your
sense of duty, even from your point of view? You're more useful—
you know you're more useful to your country here than . . ."
He stood over her, stooping a little, somehow suggesting great
gentleness and concern.
"I can't reconcile it with my conscience," he said. "In this affair there
is nothing that any man can reconcile with his conscience. I don't
mean that we oughtn't to be in this affair and on the side we're on.
We ought. But I'll put to you things I have put to no other soul."
The simplicity of his revelation seemed to her to put to shame any of
the glibnesses she had heard. It appeared to her as if a child were
speaking. He described the disillusionment it had cost him personally
as soon as this country had come into the war. He even described
the sunlit heather landscape of the north, where naïvely he had
made his tranquil resolution to join the French Foreign Legion as a
common soldier and his conviction that that would give him, as he
called it, clean bones again.
That, he said, had been straightforward. Now there was nothing
straightforward: for him or for any man. One could have fought with
a clean heart for a civilisation: if you like for the eighteenth century
against the twentieth, since that was what fighting for France
against the enemy countries meant. But our coming in had changed
the aspect at once. It was one part of the twentieth century using
the eighteenth as a catspaw to bash the other half of the twentieth.
It was true there was nothing else for it. And as long as we did it in
a decent spirit it was just bearable. One could keep at one's job—
which was faking statistics against the other fellow—until you were
sick and tired of faking and your brain reeled. And then some!
It was probably impolitic to fake—to overstate!—a case against
enemy nations. The chickens would come home to roost in one way
or another, probably. Perhaps they wouldn't. That was a matter for
one's superiors. Obviously! And the first gang had been simple,
honest fellows. Stupid, but relatively disinterested. But now! . . .
What was one to do? . . . He went on, almost mumbling. . . .
She had suddenly a clear view of him as a man extraordinarily clear-
sighted in the affairs of others, in great affairs, but in his own so
simple as to be almost a baby. And gentle! And extraordinarily
unselfish. He didn't betray one thought of self-interest . . . not one!
He was saying:
"But now! . . . with this crowd of boodlers! . . . Supposing one's
asked to manipulate the figures of millions of pairs of boots in order
to force someone else to send some miserable general and his
troops to, say, Salonika—when they and you and common-sense and
everyone and everything else, know it's disastrous? . . . And from
that to monkeying with our own forces. . . . Starving particular units
for political . . ." He was talking to himself, not to her. And indeed he
said:
"I can't, you see, talk really before you. For all I know your
sympathies, perhaps your activities, are with the enemy nations."
She said passionately:
"They're not! They're not! How dare you say such a thing?"
He answered:
"It doesn't matter . . . No! I'm sure you're not . . . But, anyhow,
these things are official. One can't, if one's scrupulous, even talk
about them . . . And then . . . You see it means such infinite deaths
of men, such an infinite prolongation . . . all this interference for
side-ends! . . . I seem to see these fellows with clouds of blood over
their heads. . . . And then . . . I'm to carry out their orders because
they're my superiors. . . . But helping them means unnumbered
deaths. . . ."
He looked at her with a faint, almost humorous smile:
"You see!" he said, "we're perhaps not so very far apart! You mustn't
think you're the only one that sees all the deaths and all the
sufferings. All, you see: I, too, am a conscientious objector. My
conscience won't let me continue any longer with these fellows. . . ."
She said:
"But isn't there any other . . ."
He interrupted:
"No! There's no other course. One is either a body or a brain in
these affairs. I suppose I'm more brain than body. I suppose so.
Perhaps I'm not. But my conscience won't let me use my brain in
this service. So I've a great, hulking body! I'll admit I'm probably not
much good. But I've nothing to live for: what I stand for isn't any
more in this world What I want, as you know, I can't have. So . . ."
She exclaimed bitterly:
"Oh, say it! Say it! Say that your large hulking body will stop two
bullets in front of two small anæmic fellows. . . . And how can you
say you'll have nothing to live for? You'll come back. You'll do your
good work again. You know you did good work . . ."
He said:
"Yes! I believe I did. I used to despise it, but I've come to believe I
did. . . . But no! They'll never let me back. They've got me out, with
all sorts of bad marks against me. They'll pursue me, systematically.
. . . You see in such a world as this, an idealist—or perhaps it's only
a sentimentalist—must be stoned to death. He makes the others so
uncomfortable. He haunts them at their golf. . . . No; they'll get me,
one way or the other. And some fellow—Macmaster here—will do my
jobs. He won't do them so well, but he'll do them more dishonestly.
Or no. I oughtn't to say dishonestly. He'll do them with enthusiasm
and righteousness. He'll fulfil the order of his superiors with an
immense docility and unction. He'll fake figures against our allies
with the black enthusiasm of a Calvin and, when that war comes,
he'll do the requisite faking with the righteous wrath of Jehovah
smiting the priests of Baal. And he'll be right. It's all we're fitted for.
We ought never to have come into this war. We ought to have
snaffled other peoples' colonies as the price of neutrality. . . ."
"Oh!" Valentine Wannop said, "how can you so hate your country?"
He said with great earnestness:
"Don't say it! Don't believe it! Don't even for a moment think it! I
love every inch of its fields and every plant in the hedgerows:
comfrey, mullein, paigles, long red purples, that liberal shepherds
give a grosser name . . . and all the rest of the rubbish—you
remember the field between the Duchemins and your mother's—and
we have always been boodlers and robbers and reivers and pirates
and cattle thieves, and so we've built up the great tradition that we
love. . . . But, for the moment, it's painful. Our present crowd is not
more corrupt than Walpole's. But one's too near them. One sees of
Walpole that he consolidated the nation by building up the National
Debt: one doesn't see his methods. . . . My son, or his son, will only
see the glory of the boodle we make out of this show. Or rather out
of the next. He won't know about the methods. They'll teach him at
school that across the counties went the sound of bugles that his
father knew. . . . Though that was another discreditable affair. . . ."
"But you!" Valentine Wannop exclaimed. "You! what will you do!
After the war!"
"I!" he said rather bewilderedly. "I! . . . Oh, I shall go into the old
furniture business. I've been offered a job. . . ."
She didn't believe he was serious. He hadn't, she knew, ever thought
about his future. But suddenly she had a vision of his white head
and pale face in the back glooms of a shop full of dusty things. He
would come out, get heavily on to a dusty bicycle and ride off to a
cottage sale. She cried out:
"Why don't you do it at once? Why don't you take the job at once?"
for in the back of the dark shop he would at least be safe.
He said:
"Oh, no! Not at this time! Besides the old furniture trade's probably
not itself for the minute. . . ." He was obviously thinking of
something else.
"I've probably been a low cad," he said, "wringing your heart with
my doubts. But I wanted to see where our similarities come in.
We've always been—or we've seemed always to me—so alike in our
thoughts. I daresay I wanted you to respect me. . . ."
"Oh, I respect you! I respect you!" she said. "You're as innocent as a
child."
He went on:
"And I wanted to get some thinking done. It hasn't been often of
late that one has had a quiet room and a fire and . . . you! To think
in front of. You do make one collect one's thoughts. I've been very
muddled till to-day . . . till five minutes ago! Do you remember our
drive? You analysed my character. I'd never have let another soul. . .
But you see . . . Don't you see?"
She said:
"No! What am I to see? I remember . . ."
He said:
"That I'm certainly not an English country gentleman now; picking
up the gossip of the horse markets and saying: let the country go to
hell, for me!"
She said:
"Did I say that? . . . Yes, I said that!"
The deep waves of emotion came over her: she trembled. She
stretched out her arms. . . . She thought she stretched out her arms.
He was hardly visible in the firelight. But she could see nothing: she
was blind for tears. She could hardly be stretching out her arms, for
she had both hands to her handkerchief on her eyes. He said
something: it was no word of love or she would have held it; it
began with: "Well, I must be . . ." He was silent for a long time: she
imagined herself to feel great waves coming from him to her. But he
wasn't in the room. . . .
The rest, till that moment at the War Office, had been pure agony,
and unrelenting. Her mother's paper cut down her money; no orders
for serials came in: her mother, obviously, was failing. The eternal
diatribes of her brother were like lashes upon her skin. He seemed
to be praying Tietjens to death. Of Tietjens she saw and heard
nothing. At the Macmasters she heard, once, that he had just gone
out. It added to her desire to scream when she saw a newspaper.
Poverty invaded them. The police raided the house in search of her
brother and his friends. Then her brother went to prison:
somewhere in the Midlands. The friendliness of their former
neighbours turned to surly suspicion. They could get no milk. Food
became almost unprocurable without going to long distances. For
three days Mrs. Wannop was clean out of her mind. Then she grew
better and began to write a new book. It promised to be rather
good. But there was no publisher. Edward came out of prison, full of
good-humour and boisterousness. They seemed to have had a great
deal to drink in prison. But, hearing that his mother had gone mad
over that disgrace, after a terrible scene with Valentine, in which he
accused her of being the mistress of Tietjens and therefore militarist,
he consented to let his mother use her influence—of which she had
still some—to get him appointed as an A.B. on a mine-sweeper.
Great winds became an agony to Valentine Wannop in addition to
the unbearable sounds of firing that came continuously over the sea.
Her mother grew much better: she took pride in having a son in a
service. She was then the more able to appreciate the fact that her
paper stopped payment altogether. A small mob on the fifth of
November burned Mrs. Wannop in effigy in front of their cottage and
broke their lower windows. Mrs. Wannop ran out and in the
illumination of the fire knocked down two farm labourer
hobbledehoys. It was terrible to see Mrs. Wannop's grey hair in the
firelight. After that the butcher refused them meat altogether, ration
card or no ration card. It was imperative that they should move to
London.
The marsh horizon became obscured with giant stilts: the air above
it filled with aeroplanes: the roads covered with military cars. There
was then no getting away from the sounds of the war.
Just as they had decided to move Tietjens came back. It was for a
moment heaven to have him in this country. But when, a month
later, Valentine Wannop saw him for a minute, he seemed very
heavy, aged and dull. It was then almost as bad as before, for it
seemed to Valentine as if he hardly had his reason.
On hearing that Tietjens was to be quartered—or, at any rate,
occupied—in the neighbourhood of Ealing, Mrs. Wannop at once
took a small house in Bedford Park, whilst, to make ends meet—for
her mother made terribly little—Valentine Wannop took a post as
athletic mistress in a great school in a not very near suburb. Thus,
though Tietjens came in for a cup of tea almost every afternoon with
Mrs. Wannop in the dilapidated little suburban house, Valentine
Wannop hardly ever saw him. The only free afternoon she had was
the Friday, and on that day she still regularly chaperoned Mrs.
Duchemin: meeting her at Charing Cross towards noon and taking
her back to the same station in time to catch the last train to Rye.
On Saturdays and Sundays she was occupied all day in typing her
mother's manuscript.
Of Tietjens, then, she saw almost nothing. She knew that his poor
mind was empty of facts and of names; but her mother said he was
a great help to her. Once provided with facts his mind worked out
sound Tory conclusions—or quite startling and attractive theories—
with extreme rapidity. This Mrs. Wannop found of the greatest use to
her whenever—though it wasn't now very often—she had an article
to write for an excitable newspaper. She still, however, contributed to
her failing organ of opinion, though it paid her nothing. . . .
Mrs. Duchemin, then, Valentine Wannop still chaperoned, though
there was no bond any more between them. Valentine knew, for
instance, perfectly well that Mrs. Duchemin, after she had been seen
off by train from Charing Cross, got out at Clapham Junction, took a
taxicab back to Gray's Inn after dark and spent the night with
Macmaster, and Mrs. Duchemin knew quite well that Valentine knew.
It was a sort of parade of circumspection and rightness, and they
kept it up even after, at a sinister registry office, the wedding had
taken place, Valentine being the one witness and an obscure-looking
substitute for the usual pew opener another. There seemed to be, by
then, no very obvious reason why Valentine should support Mrs.
Macmaster any more on these rather dreary occasions, but Mrs.
Macmaster said she might just as well, until they saw fit to make the
marriage public. There were, Mrs. Macmaster said, censorious
tongues, and even if these were confuted afterwards it is difficult, if
not impossible, to outrun scandal. Besides, Mrs. Macmaster was of
opinion that the Macmaster afternoons with these geniuses must be
a liberal education for Valentine. But, as Valentine sat most of the
time at the tea-table near the door, it was the backs and side faces
of the distinguished rather than their intellects with which she was
most acquainted. Occasionally, however, Mrs. Duchemin would show
Valentine, as an enormous privilege, one of the letters to herself
from men of genius: usually North British, written, as a rule, from
the Continent or more distant and peaceful climates, for most of
them believed it their duty in these hideous times to keep alive in
the world the only glimmering spark of beauty. Couched in terms so
eulogistic as to resemble those used in passionate love-letters by
men more profane, these epistles recounted, or consulted Mrs.
Duchemin as to, their love affairs with foreign princesses, the
progress of their ailments or the progresses of their souls towards
those higher regions of morality in which floated their so beautiful-
souled correspondent.
The letters entertained Valentine and, indeed, she was entertained
by that whole mirage. It was only the Macmaster's treatment of her
mother that finally decided Valentine that this friendship had died;
for the friendships of women are very tenacious things, surviving
astonishing disillusionments, and Valentine Wannop was a woman of
more than usual loyalty. Indeed, if she couldn't respect Mrs.
Duchemin on the old grounds, she could very really respect her for
her tenacity of purpose, her determination to advance Macmaster
and for the sort of ruthlessness that she put into these pursuits.
Valentine's affection had, indeed, survived even Edith Ethel's
continued denigrations of Tietjens—for Edith Ethel regarded Tietjens
as a clog round her husband's neck, if only because he was a very
unpopular man, grown personally rather unpresentable and always
extremely rude to the geniuses on Fridays. Edith Ethel, however,
never made these complaints that grew more and more frequent as
more and more the distinguished flocked to the Fridays, before
Macmaster. And they ceased very suddenly and in a way that struck
Valentine as odd.
Mrs. Duchemin's grievance against Tietjens was that, Macmaster
being a weak man, Tietjens had acted as his banker until, what with
interest and the rest of it, Macmaster owed Tietjens a great sum:
several thousand pounds. And there had been no real reason:
Macmaster had spent most of the money either on costly furnishings
for his rooms or on his costly journeys to Rye. On the one hand Mrs.
Duchemin could have found Macmaster all the bric-a-brac he could
possibly have wanted from amongst the things at the rectory, where
no one would have missed them and, on the other, she, Mrs.
Duchemin, would have paid all Macmaster's travelling expenses. She
had had unlimited money from her husband, who never asked for
accounts. But, whilst Tietjens still had influence with Macmaster, he
had used it uncompromisingly against this course, giving him the
delusion—it enraged Mrs. Duchemin to think!—that it would have
been dishonourable. So that Macmaster had continued to draw upon
him.
And, most enraging of all, at a period when she had had a power of
attorney over all Mr. Duchemin's fortune and could, perfectly easily,
have sold out something that no one would have missed for the
couple of thousand or so that Macmaster owed, Tietjens had very
forcibly refused to allow Macmaster to agree to anything of the sort.
He had again put into Macmaster's weak head that it would be
dishonourable. But Mrs. Duchemin—and she closed her lips
determinedly after she had said it—knew perfectly well Tietjens'
motive. So long as Macmaster owed him money he imagined that
they couldn't close their doors upon him. And their establishment
was beginning to be a place where you meet people of great
influence who might well get for a person as lazy as Tietjens a
sinecure that would suit him. Tietjens, in fact, knew which side his
bread was buttered.
For what, Mrs. Duchemin asked, could there have been
dishonourable about the arrangement she had proposed? Practically
the whole of Mr. Duchemin's money was to come to her: he was by
then insane; it was therefore, morally, her own. But immediately
after that, Mr. Duchemin having been certified, the estate had fallen
into the hands of the Lunacy Commissioners and there had been no
further hope of taking the capital. Now, her husband being dead, it
was in the hands of trustees, Mr. Duchemin having left the whole of
his property to Magdalen College and merely the income to his
widow. The income was very large; but where, with their expenses,
with the death duties and taxation, which were by then merciless,
was Mrs. Duchemin to find the money? She was to be allowed,
under her husband's will, enough capital to buy a pleasant little
place in Surrey, with rather a nice lot of land—enough to let
Macmaster know some of the leisures of a country gentleman's lot.
They were going in for shorthorns, and there was enough land to
give them a small golf-course and, in the autumn, a little—oh,
mostly rough!—shooting for Macmaster to bring his friends down to.
It would just run to that. Oh, no ostentation. Merely a nice little
place. As an amusing detail the villagers there already called
Macmaster "squire" and the women curtsied to him. But Valentine
Wannop would understand that, with all these expenses, they
couldn't find the money to pay off Tietjens. Besides, Mrs. Macmaster
said she wasn't going to pay off Tietjens. He had had his chance
once: now he could go without, for her. Macmaster would have to
pay it himself and he would never be able to, his contribution to
their housekeeping being what it was. And there were going to be
complications. Macmaster wondered about their little place in Surrey,
saying that he would consult Tietjens about this and that alteration.
But over the doorsill of that place the foot of Tietjens was never
going to go! Never! It would mean a good deal of unpleasantness;
or rather it would mean one sharp: "C-r-r-unch!" And then: Napoo
finny! Mrs. Duchemin sometimes, and with great effect,
condescended to use one of the more picturesque phrases of the
day.
To all these diatribes Valentine Wannop answered hardly anything. It
was no particular concern of her's; even if, for a moment, she felt
proprietarily towards Christopher as she did now and then, she felt
no particular desire that his intimacy with the Macmasters should be
prolonged, because she knew he could have no particular desire for
its prolongation. She imagined him turning them down with an
unspoken and good-humoured gibe. And, indeed, she agreed on the
whole with Edith Ethel. It was demoralising for a weak little man like
Vincent to have a friend with an ever-open purse beside him.
Tietjens ought not to have been princely: it was a defect, a quality
that she did not personally admire in him. As to whether it would or
wouldn't have been dishonourable for Mrs. Duchemin to take her
husband's money and give it to Macmaster, she kept an open mind.
To all intents and purposes the money was Mrs. Duchemin's, and if
Mrs. Duchemin had then paid Christopher off it would have been
sensible. She could see that later it had become very inconvenient.
There were, however, male standards to be considered, and
Macmaster, at least, passed for a man. Tietjens, who was wise
enough in the affairs of others, had, in that, probably been wise; for
there might have been great disagreeablenesses with trustees and
heirs-at-law had Mrs. Duchemin's subtraction of a couple of
thousand pounds from the Duchemin estate afterwards come to
light. The Wannops had never been large property owners as a
family, but Valentine had heard enough of collateral wranglings over
small family dishonesties to know how very disagreeable these could
be.
So she had made little or no comment; sometimes she had even
faintly agreed as to the demoralisation of Macmaster and that had
sufficed. For Mrs. Duchemin had been certain of her rightness and
cared nothing at all for the opinion of Valentine Wannop, or else took
it for granted.
And when Tietjens had been gone to France for a little time Mrs.
Duchemin seemed to forget the matter, contenting herself with
saying that he might very likely not come back. He was the sort of
clumsy man who generally got killed. In that case, since no I.O.U.s
or paper had passed, Mrs. Tietjens would have no claim. So that
would be all right.
But two days after the return of Christopher—and that was how
Valentine knew he had come back!—Mrs. Duchemin with a lowering
brow exclaimed:
"That oaf, Tietjens, is in England, perfectly safe and sound. And now
the whole miserable business of Vincent's indebtedness . . . Oh!"
She had stopped so suddenly and so markedly that even the
stoppage of Valentine's own heart couldn't conceal the oddness from
her. Indeed it was as if there were an interval before she completely
realised what the news was and as if, during that interval, she said
to herself:
"It's very queer. It's exactly as if Edith Ethel has stopped abusing
him on my account . . . As if she knew!" But how could Edith Ethel
know that she loved the man who had returned? It was impossible!
She hardly knew herself. Then the great wave of relief rolled over
her: he was in England. One day she would see him, there: in the
great room. For these colloquies with Edith Ethel always took place
in the great room where she had last seen Tietjens. It looked
suddenly beautiful and she was resigned to sitting there, waiting for
the distinguished.
It was indeed a beautiful room: it had become so during the years.
It was long and high—matching the Tietjens'. A great cut-glass
chandelier from the rectory hung dimly coruscating in the centre,
reflected and re-reflected in convex gilt mirrors, topped by eagles. A
great number of books had gone to make place on the white
panelled walls for the mirrors, and for the fair orange and brown
pictures by Turner, also from the rectory. From the rectory had come
the immense scarlet and lapis lazuli carpet, the great brass fire-
basket and appendages, the great curtains that, in the three long
windows, on their peacock blue Chinese silk showed parti-coloured
cranes ascending in long flights—and all the polished Chippendale
arm-chairs. Amongst all these, gracious, trailing, stopping with a
tender gesture to rearrange very slightly the crimson roses in the
famous silver bowls, still in dark blue silks, with an amber necklace
and her elaborate black hair, waved exactly like that of Julia Domna
of the Musée Lapidaire at Arles, moved Mrs. Macmaster—also from
the rectory. Macmaster had achieved his desire: even to the
shortbread cakes and the peculiarly scented tea that came every
Friday morning from Princes Street. And, if Mrs. Macmaster hadn't
the pawky, relishing humour of the great Scots ladies of past days,
she had in exchange her deep aspect of comprehension and
tenderness. An astonishingly beautiful and impressive woman: dark
hair; dark, straight eyebrows; a straight nose; dark blue eyes in the
shadows of her hair and bowed, pomegranate lips in a chin curved
like the bow of a Greek boat. . . .
The etiquette of the place on Fridays was regulated as if by a royal
protocol. The most distinguished and, if possible, titled person was
led to a great walnut-wood fluted chair that stood askew by the
fireplace, its back and seat of blue velvet, heaven knows how old.
Over him would hover Mrs. Duchemin: or, if he were very
distinguished, both Mr. and Mrs. Macmaster. The not so distinguished
were led up by turns to be presented to the celebrity and would then
arrange themselves in a half-circle in the beautiful arm-chairs; the
less distinguished still, in outer groups in chairs that had no arms:
the almost undistinguished stood, also in groups or languished,
awestruck on the scarlet leather window seats. When all were there
Macmaster would establish himself on the incredibly unique
hearthrug and would address wise sayings to the celebrity;
occasionally, however, saying a kind thing to the youngest man
present—to give him a chance of distinguishing himself. Macmaster's
hair, at that date, was still black, but not quite so stiff or so well
brushed; his beard had in it greyish streaks and his teeth, not being
quite so white, looked less strong. He wore also a single eyeglass,
the retaining of which in his right eye gave him a slightly agonised
expression. It gave him, however, the privilege of putting his face
very close to the face of anyone upon whom he wished to make a
deep impression. He had lately become much interested in the
drama, so that there were usually several large—and, of course, very
reputable and serious actresses in the room. On rare occasions Mrs.
Duchemin would say across the room in her deep voice:
"Valentine, a cup of tea for his highness," or "Sir Thomas," as the
case might be, and when Valentine had threaded her way through
the chairs with a cup of tea Mrs. Duchemin, with a kind, aloof smile,
would say: "Your highness, this is my little brown bird." But as a rule
Valentine sat alone at the tea-table, the guests fetching from her
what they wanted.
Tietjens came to the Fridays twice during the five months of his stay
at Ealing. On each occasion he accompanied Mrs. Wannop.
In earlier days—during the earliest Fridays—Mrs. Wannop, if she
ever came, had always been installed, with her flowing black, in the
throne and, like an enlarged Queen Victoria, had sat there whilst
suppliants were led up to this great writer. But now: on the first
occasion Mrs. Wannop got a chair without arms in the outer ring,
whilst a general officer commanding lately in chief somewhere in the
East, whose military success had not been considerable, but whose
despatches were considered very literary, occupied, rather blazingly,
the throne. But Mrs. Wannop had chatted very contentedly all the
afternoon with Tietjens, and it had been comforting to Valentine to
see Tietjens' large, uncouth, but quite collected figure, and to
observe the affection that these two had for each other.
But, on the second occasion, the throne was occupied by a very
young woman who talked a great deal and with great assurance.
Valentine didn't know who she was. Mrs. Wannop, very gay and
distracted, stood nearly the whole afternoon by a window. And even
at that, Valentine was contented, quite a number of young men
crowding round the old lady and leaving the younger one's circle
rather bare.
There came in a very tall, clean run and beautiful, fair woman,
dressed in nothing in particular. She stood with extreme—with
noticeable—unconcern near the doorway. She let her eyes rest on
Valentine, but looked away before Valentine could speak. She must
have had an enormous quantity of fair tawny hair, for it was coiled in
a great surface over her ears. She had in her hand several visiting
cards which she looked at with a puzzled expression and then laid on
a card table. She was no one who had ever been there before.
Edith Ethel—it was for the second time!—had just broken up the ring
that surrounded Mrs. Wannop, bearing the young men tributary to
the young women in the walnut chair and leaving Tietjens and the
older woman high and dry in a window: thus Tietjens saw the
stranger, and there was no doubt left in Valentine's mind. He came,
diagonally, right down the room to his wife and marched her straight
up to Edith Ethel. His face was perfectly without expression.
Macmaster, perched on the centre of the hearthrug, had an emotion
that was extraordinarily comic to witness, but that Valentine was
quite unable to analyse. He jumped two paces forward to meet Mrs.
Tietjens, held out a little hand, half withdrew it, retreated half a
step. The eyeglass fell from his perturbed eye: this gave him actually
an expression less perturbed, but, in revenge, the hairs on the back
of his scalp grew suddenly untidy. Sylvia, wavering along beside her
husband, held out her long arm and careless hand. Macmaster
winced almost at the contact, as if his fingers had been pinched in a
vice. Sylvia wavered desultorily towards Edith Ethel, who was
suddenly small, insignificant and relatively coarse. As for the young
woman celebrity in the arm-chair, she appeared to be about the size
of a white rabbit.
A complete silence had fallen on the room. Every woman in it was
counting the pleats of Sylvia's skirt and the amount of material in it.
Valentine Wannop knew that because she was doing it herself. If one
had that amount of material and that number of pleats one's skirt
might hang like that. . . . For it was extraordinary: it fitted close
round the hips, and gave an effect of length and swing—yet it did
not descend as low as the ankles. It was, no doubt, the amount of
material that did that, like the Highlander's kilt that takes twelve
yards to make. And from the silence Valentine could tell that every
woman and most of the men—if they didn't know that this was Mrs.
Christopher Tietjens—knew that this was a personage of Illustrated
Weekly, as who should say of county family, rank. Little Mrs. Swan,
lately married, actually got up, crossed the room and sat down
beside her bridegroom. It was a movement with which Valentine
could sympathise.
And Sylvia, having just faintly greeted Mrs. Duchemin, and
completely ignored the celebrity in the arm-chair—in spite of the fact
that Mrs. Duchemin had tried half-heartedly to effect an introduction
—stood still, looking round her. She gave the effect of a lady in a
nurseryman's hot-house considering what flower should interest her,
collectedly ignoring the nurserymen who bowed round her. She had
just dropped her eyelashes, twice, in recognition of two staff officers
with a good deal of scarlet streak about them who were tentatively
rising from their chairs. The staff officers who came to the
Macmasters were not of the first vintages; still they had the labels
and passed as such.
Valentine was by that time beside her mother, who had been
standing all alone between two windows. She had dispossessed, in
hot indignation, a stout musical critic of his chair and had sat her
mother in it. And, just as Mrs. Duchemin's deep voice sounded, yet a
little waveringly:
"Valentine . . . a cup of tea for . . ." Valentine was carrying a cup of
tea to her mother.
Her indignation had conquered her despairing jealousy, if you could
call it jealousy. For what was the good of living or loving when
Tietjens had beside him, for ever, the radiant, kind and gracious
perfection. On the other hand, of her two deep passions, the second
was for her mother.
Rightly or wrongly, Valentine regarded Mrs. Wannop as a great, an
august figure: a great brain, a high and generous intelligence. She
had written, at least, one great book, and if the rest of her time had
been frittered away in the desperate struggle to live that had taken
both their lives, that could not detract from that one achievement
that should last and for ever take her mother's name down time.
That this greatness should not weigh with the Macmasters had
hitherto neither astonished nor irritated Valentine. The Macmasters
had their game to play and, for the matter of that, they had their
predilections. Their game kept them amongst the officially
influential, the semi-official and the officially accredited. They moved
with such C.B.s, knights, presidents, and the rest as dabbled in
writing or the arts: they went upwards with such reviewers, art
critics, musical writers and archæologists as had posts in, if possible,
first-class public offices or permanent positions on the more august
periodicals. If an imaginative author seemed assured of position and
lasting popularity Macmaster would send out feelers towards him,
would make himself humbly useful, and sooner or later either Mrs.
Duchemin would be carrying on with him one of her high-souled
correspondences—or she wouldn't.
Mrs. Wannop they had formerly accepted as permanent leader writer
and chief critic of a great organ, but the great organ having
dwindled and now disappeared the Macmasters no longer wanted
her at their parties. That was the game—and Valentine accepted it.
But that it should have been done with such insolence, so obviously
meant to be noted—for in twice breaking up Mrs. Wannop's little
circle Mrs. Duchemin had not even once so much as said: "How d'ye
do?" to the elder lady!—that was almost more than Valentine could,
for the moment, bear, and she would have taken her mother away at
once and would never have re-entered the house, but for the
compensations.
Her mother had lately written and even found a publisher for a book
—and the book had showed no signs of failing powers. On the
contrary, having been perforce stopped off the perpetual journalism
that had dissipated her energies, Mrs. Wannop had turned out
something that Valentine knew was sound, sane and well done.
Abstractions caused by failing attention to the outside world are not
necessarily in a writer signs of failing, as a writer. It may mean
merely that she is giving so much thought to her work that her other
contacts suffer. If that is the case her work will gain. That this might
be the case with her mother was Valentine's great and secret hope.
Her mother was barely sixty: many great works have been written
by writers aged between sixty and seventy. . . .
And the crowding of youngish men round the old lady had given
Valentine a little confirmation of that hope. The book naturally, in the
maelstrom flux and reflux of the time, had attracted little attention,
and poor Mrs. Wannop had not succeeded in extracting a penny for
it from her adamantine publisher: she hadn't, indeed, made a penny
for several months, and they existed almost at starvation point in
their little den of a villa—on Valentine's earnings as athletic teacher. .
. . But that little bit of attention in that semi-public place had
seemed, at least, as a confirmation to Valentine: there probably was
something sound, sane and well done in her mother's work. That
was almost all she asked of life.
And, indeed, whilst she stood by her mother's chair, thinking with a
little bitter pathos that if Edith Ethel had left the three or four young
men to her mother the three or four might have done her poor
mother a little good, with innocent puffs and the like—and heaven
knew they needed that little good badly enough!—a very thin and
untidy young man did drift back to Mrs. Wannop and asked,
precisely, if he might make a note or two for publication as to what
Mrs. Wannop was doing. "Her book," he said, "had attracted so
much attention. They hadn't known that they had still writers among
them. . . ."
A singular, triangular drive had begun through the chairs from the
fireplace. That was how it had seemed to Valentine! Mrs. Tietjens
had looked at them, had asked Christopher a question and,
immediately, as if she were coming through waist-high surf, had
borne down Macmaster and Mrs. Duchemin, flanking her
obsequiously, setting aside chairs and their occupants, Tietjens and
the two, rather bashfully following staff officers, broadening out the
wedge.
Sylvia, her long arm held out from a yard or so away, was giving her
hand to Valentine's mother. With her clear, high, unembarrassed
voice she exclaimed, also from a yard or so away, so as to be heard
by every one in the room:
"You're Mrs. Wannop. The great writer! I'm Christopher Tietjens'
wife."
The old lady, with her dim eyes, looked up at the younger woman
towering above her.
"You're Christopher's wife!" she said. "I must kiss you for all the
kindness he has shown me."
Valentine felt her eyes filling with tears. She saw her mother stand
up, place both her hands on the other woman's shoulders. She
heard her mother say:
"You're a most beautiful creature. I'm sure you're good!"
Sylvia stood, smiling faintly, bending a little to accept the embrace.
Behind the Macmasters, Tietjens and the staff officers, a little crowd
of goggle eyes had ranged itself.
Valentine was crying. She slipped back behind the tea-urns, though
she could hardly feel the way. Beautiful! The most beautiful woman
she had ever seen! And good! Kind! You could see it in the lovely
way she had given her cheek to that poor old woman's lips. . . . And
to live all day, for ever, beside him . . . she, Valentine, ought to be
ready to lay down her life for Sylvia Tietjens. . . .
The voice of Tietjens said, just above her head:
"Your mother seems to be having a regular triumph," and, with his
good-natured cynicism, he added, "it seems to have upset some
apple-carts!" They were confronted with the spectacle of Macmaster
conducting the young celebrity from her deserted arm-chair across
the room to be lost in the horseshoe of crowd that surrounded Mrs.
Wannop.
Valentine said:
"You're quite gay to-day. Your voice is different. I suppose you're
better?" She did not look at him. His voice came:
"Yes! I'm relatively gay!" It went on: "I thought you might like to
know. A little of my mathematical brain seems to have come to life
again. I've worked out two or three silly problems. . . ."
She said:
"Mrs. Tietjens will be pleased."
"Oh!" the answer came. "Mathematics don't interest her any more
than cock-fighting." With immense swiftness, between word and
word, Valentine read into that a hope! This splendid creature did not
sympathise with her husband's activities. But he crushed it heavily
by saying: "Why should she? She's so many occupations of her own
that she's unrivalled at!"
He began to tell her, rather minutely, of a calculation he had made
only that day at lunch. He had gone into the Department of Statistics
and had had rather a row with Lord Ingleby of Lincoln. A pretty title
the fellow had taken! They had wanted him to ask to be seconded to
his old department for a certain job. But he had said he'd be
damned if he would. He detested and despised the work they were
doing.
Valentine, for the first time in her life, hardly listened to what he
said. Did the fact that Sylvia Tietjens had so many occupations of
her own mean that Tietjens found her unsympathetic? Of their
relationships she knew nothing. Sylvia had been so much of a
mystery as hardly to exist as a problem hitherto. Macmaster,
Valentine knew, hated her. She knew that through Mrs. Duchemin;
she had heard it ages ago, but she didn't know why. Sylvia had
never come to the Macmaster afternoons; but that was natural.
Macmaster passed for a bachelor, and it was excusable for a young
woman of the highest fashion not to come to bachelor teas of
literary and artistic people. On the other hand, Macmaster dined at
the Tietjens quite often enough to make it public that he was a
friend of that family. Sylvia, too, had never come down to see Mrs.
Wannop. But then it would, in the old days, have been a long way to
come for a lady of fashion with no especial literary interests. And no
one, in mercy, could have been expected to call on poor them in
their dog kennel in an outer suburb. They had had to sell almost all
their pretty things.
Tietjens was saying that after his tempestuous interview with Lord
Ingleby of Lincoln—she wished he would not be so rude to powerful
people!—he had dropped in on Macmaster in his private room, and
finding him puzzled over a lot of figures had, in the merest spirit of
bravado, taken Macmaster and his papers out to lunch. And, he said,
chancing to look, without any hope at all, at the figures, he had
suddenly worked out an ingenious mystification. It had just come!
His voice had been so gay and triumphant that she hadn't been able
to resist looking up at him. His cheeks were fresh coloured, his hair
shining; his blue eyes had a little of their old arrogance—and
tenderness! Her heart seemed to sing with joy! He was, she felt, her
man. She imagined the arms of his mind stretching out to enfold her.
He went on explaining. He had rather, in his recovered self-
confidence, gibed at Macmaster. Between themselves, wasn't it easy
to do what the Department, under orders, wanted done? They had
wanted to rub into our allies that their losses by devastation had
been nothing to write home about—so as to avoid sending
reinforcements to their lines! Well, if you took just the bricks and
mortar of the devastated districts, you could prove that the loss in
bricks, tiles, woodwork and the rest didn't—and the figures with a
little manipulation would prove it!—amount to more than a normal
year's dilapidations spread over the whole country in peace time. . . .
House repairs in a normal year had cost several million sterling. The
enemy had only destroyed just about so many million sterling in
bricks and mortar. And what was a mere year's dilapidations in
house property! You just neglected to do them and did them next
year.
So, if you ignored the lost harvests of three years, the lost industrial
output of the richest industrial region of the country, the smashed
machinery, the barked fruit trees, the three years' loss of four and a
half-tenths of the coal output for three years—and the loss of life!—
we could go to our allies and say:
"All your yappings about losses are the merest bulls. You can
perfectly well afford to reinforce the weak places of your own lines.
We intend to send our new troops to the Near East, where lies our
true interest!" And, though they might sooner or later point out the
fallacy, you would by so much have put off the abhorrent expedient
of a single command.
Valentine, though it took her away from her own thoughts, couldn't
help saying:
"But weren't you arguing against your own convictions?"
He said:
"Yes, of course I was. In the lightness of my heart! It's always a
good thing to formulate the other fellow's objections."
She had turned half round in her chair. They were gazing into each
other's eyes, he from above, she from below. She had no doubt of
his love: he, she knew, could have no doubt of hers. She said:
"But isn't it dangerous? To show these people how to do it?"
He said:
"Oh, no, no. No! You don't know what a good soul little Vinnie is. I
don't think you've ever been quite just to Vincent Macmaster! He'd
as soon think of picking my pocket as of picking my brains. The soul
of honour!"
Valentine had felt a queer, queer sensation. She was not sure
afterwards whether she had felt it before she had realised that Sylvia
Tietjens was looking at them. She stood there, very erect, a queer
smile on her face. Valentine could not be sure whether it was kind,
cruel, or merely distantly ironic; but she was perfectly sure it
showed, whatever was behind it, that its wearer knew all that there
was to know of her, Valentine's, feelings for Tietjens and for Tietjens'
feelings for her. . . . It was like being a woman and man in adultery
in Trafalgar Square.
Behind Sylvia's back, their mouths agape, were the two staff
officers. Their dark hairs were too untidy for them to amount to
much, but, such as they were, they were the two most presentable
males of the assembly—and Sylvia had snaffled them.
Mrs. Tietjens said:
"Oh, Christopher! I'm going on to the Basil's."
Tietjens said:
"All right. I'll pop Mrs. Wannop into the tube as soon as she's had
enough of it, and come along and pick you up!"
Sylvia had just drooped her long eyelashes, in sign of salutation, to
Valentine Wannop, and had drifted through the door, followed by her
rather unmilitary military escort in khaki and scarlet.
From that moment Valentine Wannop never had any doubt. She
knew that Sylvia Tietjens knew that her husband loved her, Valentine
Wannop, and that she, Valentine Wannop, loved her husband—with
a passion absolute and ineffable. The one thing she, Valentine, didn't
know, the one mystery that remained impenetrable, was whether
Sylvia Tietjens was good to her husband!
A long time afterwards Edith Ethel had come to her beside the tea-
cups and had apologised for not having known, earlier than Sylvia's
demonstration, that Mrs. Wannop was in the room. She hoped that
they might see Mrs. Wannop much more often. She added after a
moment that she hoped Mrs. Wannop wouldn't, in future, find it
necessary to come under the escort of Mr. Tietjens. They were too
old friends for that, surely.
Valentine said:
"Look here, Ethel, if you think that you can keep friends with mother
and turn on Mr. Tietjens after all he's done for you, you're mistaken.
You are really. And mother's a great deal of influence. I don't want
to see you making any mistakes: just at this juncture. It's a mistake
to make nasty rows. And you'd make a very nasty one if you said
anything against Mr. Tietjens to mother. She knows a great deal.
Remember. She lived next door to the rectory for a number of years.
And she's got a dreadfully incisive tongue. . . ."
Edith Ethel coiled back on her feet as if her whole body were
threaded by a steel spring. Her mouth opened, but she bit her lower
lip and then wiped it with a very white handkerchief. She said:
"I hate that man! I detest that man! I shudder when he comes near
me."
"I know you do!" Valentine Wannop answered. "But I wouldn't let
other people know it if I were you. It doesn't do you any real credit.
He's a good man."
Edith Ethel looked at her with a long, calculating glance. Then she
went to stand before the fireplace.
That had been five—or at most six—Fridays before Valentine sat with
Mark Tietjens in the War Office waiting hall, and, on the Friday
immediately before that again, all the guests being gone, Edith Ethel
had come to the tea-table and, with her velvet kindness, had placed
her right hand on Valentine's left. Admiring the gesture with a deep
fervour, Valentine knew that that was the end.
Three days before, on the Monday, Valentine, in her school uniform,
in a great store to which she had gone to buy athletic paraphernalia,
had run into Mrs. Duchemin, who was buying flowers. Mrs.
Duchemin had been horribly distressed to observe the costume. She
had said:
"But do you go about in that? It's really dreadful." Valentine had
answered:
"Oh, yes. When I'm doing business for the school in school hours I'm
expected to wear it. And I wear it if I'm going anywhere in a hurry
after school hours. It saves my dresses. I haven't got too many."
"But any one might meet you," Edith Ethel said in a note of agony.
"It's very inconsiderate. Don't you think you've been very
inconsiderate? You might meet any of the people who come to our
Fridays!"
"I frequently do," Valentine said. "But they don't seem to mind.
Perhaps they think I'm a Waac officer. That would be quite
respectable. . . ."
Mrs. Duchemin drifted away, her arms full of flowers and real agony
upon her face.
Now, beside the tea-table she said, very softly:
"My dear, we've decided not to have our usual Friday afternoon next
week." Valentine wondered whether this was merely a lie to get rid
of her. But Edith Ethel went on: "We've decided to have a little
evening festivity. After a great deal of thought we've come to the
conclusion that we ought, now, to make our union public." She
paused to await comment, but Valentine making none she went on:
"It coincides very happily—I can't help feeling it coincides very
happily!—with another event. Not that we set much store by these
things. . . . But it has been whispered to Vincent that next Friday. . .
. Perhaps, my dear Valentine, you, too, will have heard . . ."
Valentine said:
"No, I haven't. I suppose he's got the O.B.E. I'm very glad."
"The Sovereign," Mrs. Duchemin said, "is seeing fit to confer the
honour of knighthood on him."
"Well!" Valentine said. "He's had a quick career. I've no doubt he
deserves it. He's worked very hard. I do sincerely congratulate you.
It'll be a great help to you."
"It's," Mrs. Duchemin said, "not for mere plodding. That's what
makes it so gratifying. It's for a special piece of brilliance, that has
marked him out. It's, of course, a secret. But . . ."
"Oh, I know!" Valentine said. "He's worked out some calculations to
prove that losses in the devastated districts, if you ignore machinery,
coal output, orchard trees, harvests, industrial products and so on,
don't amount to more than a year's household dilapidations for the .
. ."
Mrs. Duchemin said with real horror:
"But how did you know? How on earth did you know? . . ." She
paused. "It's such a dead secret. . . . That fellow must have told
you. . . . But how on earth could he know?"
"I haven't seen Mr. Tietjens to speak to since the last time he was
here," Valentine said. She saw, from Edith Ethel's bewilderment, the
whole situation. The miserable Macmaster hadn't even confided to
his wife that the practically stolen figures weren't his own. He
desired to have a little prestige in the family circle; for once a little
prestige! Well! Why shouldn't he have it? Tietjens, she knew, would
wish him to have all he could get. She said therefore:
"Oh, it's probably in the air. . . . It's known the Government want to
break their claims to the higher command. And anyone who could
help them to that would get a knighthood. . . ."
Mrs. Duchemin was more calm.
"It's certainly," she said, "Burke'd, as you call it, those beastly
people." She reflected for a moment. "It's probably that," she went
on. "It's in the air. Anything that can help to influence public opinion
against those horrible people is to be welcomed. That's known pretty
widely. . . . No! It could hardly be Christopher Tietjens who thought
of it and told you. It wouldn't enter his head. He's their friend! He
would be . . ."
"He's certainly," Valentine said, "not a friend of his country's
enemies. I'm not myself."
Mrs. Duchemin exclaimed sharply, her eyes dilated.
"What do you mean? What on earth do you dare to mean? I thought
you were a pro-German!"
Valentine said:
"I'm not! I'm not! . . . I hate men's deaths. . . . I hate any men's
deaths. . . . Any men . . ." She calmed herself by main force. "Mr.
Tietjens says that the more we hinder our allies the more we drag
the war on and the more lives are lost. . . . More lives, do you
understand? . . ."
Mrs. Duchemin assumed her most aloof, tender and high air: "My
poor child," she said, "what possible concern can the opinions of
that broken fellow cause anyone? You can warn him from me that he
does himself no good by going on uttering these discredited
opinions. He's a marked man. Finished! It's no good Guggums, my
husband, trying to stand up for him."
"He does stand up for him?" Valentine asked. "Though I don't see
why it's needed. Mr. Tietjens is surely able to take care of himself."
"My good child," Edith Ethel said, "you may as well know the worst.
There's not a more discredited man in London than Christopher
Tietjens, and my husband does himself infinite harm in standing up
for him. It's our one quarrel."
She went on again:
"It was all very well whilst that fellow had brains. He was said to
have some intellect, though I could never see it. But now that, with
his drunkenness and debaucheries, he has got himself into the state
he is in; for there's no other way of accounting for his condition!
They're striking him, I don't mind telling you, off the roll of his office.
. . ."
It was there that, for the first time, the thought went through
Valentine Wannop's mind, like a mad inspiration: this woman must at
one time have been in love with Tietjens. It was possible, men being
what they were that she had even once been Tietjens' mistress. For
it was impossible otherwise to account for this spite, which to
Valentine seemed almost meaningless. She had, on the other hand,
no impulse to defend Tietjens against accusations that could not
have any possible grounds.
Mrs. Duchemin was going on with her kind loftiness:
"Of course a fellow like that—in that condition!—could not
understand matters of high policy. It is imperative that these fellows
should not have the higher command. It would pander to their
insane spirit of militarism. They must be hindered. I'm talking, of
course, between ourselves, but my husband says that that is the
conviction in the very highest circles. To let them have their way,
even if it led to earlier success, would be to establish a precedent—
so my husband says!—compared with which the loss of a few lives. .
. ."
Valentine sprang up, her face distorted.
"For the sake of Christ," she cried out, "as you believe that Christ
died for you, try to understand that millions of men's lives are at
stake. . . ."
Mrs. Duchemin smiled.
"My poor child," she said, "if you moved in the higher circles you
would look at these things with more aloofness. . . ."
Valentine leant on the back of a high chair for support.
"You don't move in the higher circles," she said. "For Heaven's sake
—for your own—remember that you are a woman, not for ever and
for always a snob. You were a good woman once. You stuck to your
husband for quite a long time. . . ."
Mrs. Duchemin, in her chair, had thrown herself back.
"My good girl," she said, "have you gone mad?"
Valentine said:
"Yes, very nearly. I've got a brother at sea; I've had a man I loved
out there for an infinite time. You can understand that, I suppose,
even if you can't understand how one can go mad merely at the
thoughts of suffering at all. . . . And I know, Edith Ethel, that you are
afraid of my opinion of you, or you wouldn't have put up all the
subterfuges and concealments of all these years. . . ."
Mrs. Duchemin said quickly:
"Oh, my good girl. . . . If you've got personal interests at stake you
can't be expected to take abstract views of the higher matters. We
had better change the subject."
Valentine said:
"Yes, do. Get on with your excuses for not asking me and mother to
your knighthood party."
Mrs. Duchemin, too, rose at that. She felt at her amber beads with
long fingers that turned very slightly at the tips. She had behind her
all her mirrors, the drops of her lustres, shining points of gilt and of
the polish of dark woods. Valentine thought that she had never seen
anyone so absolutely impersonate kindness, tenderness and dignity.
She said:
"My dear, I was going to suggest that it was the sort of party to
which you might not care to come. . . . The people will be stiff and
formal and you probably haven't got a frock."
Valentine said:
"Oh, I've got a frock all right. But there's a Jacob's ladder in my
party stockings and that's the sort of ladder you can't kick down."
She couldn't help saying that.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

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

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookluna.com

You might also like