S e E v dien ti t o h n JavaScript The Definitive Guide Master the World's Most-Used Programming Language David Flanagan Praise for JavaScript: The Definitive Guide, Seventh Edition “This book is everything you never knew you wanted to know about JavaScript. Take your JavaScript code quality and productivity to the next level. David’s knowledge of the language, its intricacies and gotchas, is astounding, and it shines through in this truly definitive guide to the JavaScript language.” —Schalk Neethling, Senior Frontend Engineer at MDN Web Docs “David Flanagan takes readers on a guided tour of JavaScript that will provide them with a feature-complete picture of the language and its ecosystem.” —Sarah Wachs, Frontend Developer and Women Who Code Berlin Lead “Any developer interested in being productive in codebases developed throughout JavaScript’s lifetime (including the latest and emerging features) will be well served by a deep and reflective journey through this comprehensive and definitive book.” —Brian Sletten, President of Bosatsu Consulting SEVENTH EDITION JavaScript: The Definitive Guide Master the World’s Most-Used Programming Language David Flanagan BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo JavaScript: The Definitive Guide, Seventh Edition by David Flanagan Copyright © 2020 David Flanagan. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Acquisitions Editor: Jennifer Pollock Indexer: Judith McConville Development Editor: Angela Rufino Interior Designer: David Futato Production Editor: Deborah Baker Cover Designer: Karen Montgomery Copyeditor: Holly Bauer Forsyth Illustrator: Rebecca Demarest Proofreader: Piper Editorial, LLC June 1998: Third Edition November 2001: Fourth Edition August 2006: Fifth Edition May 2011: Sixth Edition May 2020: Seventh Edition Revision History for the Seventh Edition 2020-05-13: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491952023 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. JavaScript: The Definitive Guide, Sev‐ enth Edition, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-95202-3 [LSI] To my parents, Donna and Matt, with love and gratitude. Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Introduction to JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Exploring JavaScript 3 1.2 Hello World 5 1.3 A Tour of JavaScript 5 1.4 Example: Character Frequency Histograms 11 1.5 Summary 14 2. Lexical Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.1 The Text of a JavaScript Program 15 2.2 Comments 16 2.3 Literals 16 2.4 Identifiers and Reserved Words 16 2.5 Unicode 17 2.6 Optional Semicolons 19 2.7 Summary 21 3. Types, Values, and Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.1 Overview and Definitions 23 3.2 Numbers 25 3.3 Text 32 3.4 Boolean Values 38 3.5 null and undefined 40 3.6 Symbols 41 3.7 The Global Object 42 3.8 Immutable Primitive Values and Mutable Object References 43 3.9 Type Conversions 45 vii 3.10 Variable Declaration and Assignment 53 3.11 Summary 60 4. Expressions and Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.1 Primary Expressions 62 4.2 Object and Array Initializers 62 4.3 Function Definition Expressions 63 4.4 Property Access Expressions 64 4.5 Invocation Expressions 66 4.6 Object Creation Expressions 68 4.7 Operator Overview 68 4.8 Arithmetic Expressions 73 4.9 Relational Expressions 78 4.10 Logical Expressions 84 4.11 Assignment Expressions 86 4.12 Evaluation Expressions 88 4.13 Miscellaneous Operators 91 4.14 Summary 96 5. Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.1 Expression Statements 98 5.2 Compound and Empty Statements 99 5.3 Conditionals 100 5.4 Loops 105 5.5 Jumps 112 5.6 Miscellaneous Statements 121 5.7 Declarations 124 5.8 Summary of JavaScript Statements 127 6. Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 6.1 Introduction to Objects 129 6.2 Creating Objects 130 6.3 Querying and Setting Properties 133 6.4 Deleting Properties 138 6.5 Testing Properties 139 6.6 Enumerating Properties 140 6.7 Extending Objects 142 6.8 Serializing Objects 143 6.9 Object Methods 144 6.10 Extended Object Literal Syntax 146 6.11 Summary 153 viii | Table of Contents