JavaScript: The Definitive Guide, 7th Edition

JavaScript: The Definitive Guide, 7th Edition

Read it now on the O’Reilly learning platform with a 10-day free trial.

O’Reilly members get unlimited access to books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.

Book description

JavaScript is the programming language of the web and is used by more software developers today than any other programming language. For nearly 25 years this best seller has been the go-to guide for JavaScript programmers. The seventh edition is fully updated to cover the 2020 version of JavaScript, and new chapters cover classes, modules, iterators, generators, Promises, async/await, and metaprogramming. You’ll find illuminating and engaging example code throughout.

This book is for programmers who want to learn JavaScript and for web developers who want to take their understanding and mastery to the next level. It begins by explaining the JavaScript language itself, in detail, from the bottom up. It then builds on that foundation to cover the web platform and Node.js.

Show and hide more

Publisher resources

Table of contents Product information

Table of contents

  1. Preface
    1. Conventions Used in This Book
    2. Example Code
    3. O’Reilly Online Learning
    4. How to Contact Us
    5. Acknowledgments
    1. 1.1 Exploring JavaScript
    2. 1.2 Hello World
    3. 1.3 A Tour of JavaScript
    4. 1.4 Example: Character Frequency Histograms
    5. 1.5 Summary
    1. 2.1 The Text of a JavaScript Program
    2. 2.2 Comments
    3. 2.3 Literals
    4. 2.4 Identifiers and Reserved Words
      1. 2.4.1 Reserved Words
      1. 2.5.1 Unicode Escape Sequences
      2. 2.5.2 Unicode Normalization
      1. 3.1 Overview and Definitions
      2. 3.2 Numbers
        1. 3.2.1 Integer Literals
        2. 3.2.2 Floating-Point Literals
        3. 3.2.3 Arithmetic in JavaScript
        4. 3.2.4 Binary Floating-Point and Rounding Errors
        5. 3.2.5 Arbitrary Precision Integers with BigInt
        6. 3.2.6 Dates and Times
        1. 3.3.1 String Literals
        2. 3.3.2 Escape Sequences in String Literals
        3. 3.3.3 Working with Strings
        4. 3.3.4 Template Literals
        5. 3.3.5 Pattern Matching
        1. 3.9.1 Conversions and Equality
        2. 3.9.2 Explicit Conversions
        3. 3.9.3 Object to Primitive Conversions
        1. 3.10.1 Declarations with let and const
        2. 3.10.2 Variable Declarations with var
        3. 3.10.3 Destructuring Assignment
        1. 4.1 Primary Expressions
        2. 4.2 Object and Array Initializers
        3. 4.3 Function Definition Expressions
        4. 4.4 Property Access Expressions
          1. 4.4.1 Conditional Property Access
          1. 4.5.1 Conditional Invocation
          1. 4.7.1 Number of Operands
          2. 4.7.2 Operand and Result Type
          3. 4.7.3 Operator Side Effects
          4. 4.7.4 Operator Precedence
          5. 4.7.5 Operator Associativity
          6. 4.7.6 Order of Evaluation
          1. 4.8.1 The + Operator
          2. 4.8.2 Unary Arithmetic Operators
          3. 4.8.3 Bitwise Operators
          1. 4.9.1 Equality and Inequality Operators
          2. 4.9.2 Comparison Operators
          3. 4.9.3 The in Operator
          4. 4.9.4 The instanceof Operator
          1. 4.10.1 Logical AND (&&)
          2. 4.10.2 Logical OR (||)
          3. 4.10.3 Logical NOT (!)
          1. 4.11.1 Assignment with Operation
          1. 4.12.1 eval()
          2. 4.12.2 Global eval()
          3. 4.12.3 Strict eval()
          1. 4.13.1 The Conditional Operator (?:)
          2. 4.13.2 First-Defined (??)
          3. 4.13.3 The typeof Operator
          4. 4.13.4 The delete Operator
          5. 4.13.5 The await Operator
          6. 4.13.6 The void Operator
          7. 4.13.7 The comma Operator (,)
          1. 5.1 Expression Statements
          2. 5.2 Compound and Empty Statements
          3. 5.3 Conditionals
            1. 5.3.1 if
            2. 5.3.2 else if
            3. 5.3.3 switch
            1. 5.4.1 while
            2. 5.4.2 do/while
            3. 5.4.3 for
            4. 5.4.4 for/of
            5. 5.4.5 for/in
            1. 5.5.1 Labeled Statements
            2. 5.5.2 break
            3. 5.5.3 continue
            4. 5.5.4 return
            5. 5.5.5 yield
            6. 5.5.6 throw
            7. 5.5.7 try/catch/finally
            1. 5.6.1 with
            2. 5.6.2 debugger
            3. 5.6.3 “use strict”
            1. 5.7.1 const, let, and var
            2. 5.7.2 function
            3. 5.7.3 class
            4. 5.7.4 import and export
            1. 6.1 Introduction to Objects
            2. 6.2 Creating Objects
              1. 6.2.1 Object Literals
              2. 6.2.2 Creating Objects with new
              3. 6.2.3 Prototypes
              4. 6.2.4 Object.create()
              1. 6.3.1 Objects As Associative Arrays
              2. 6.3.2 Inheritance
              3. 6.3.3 Property Access Errors
              1. 6.6.1 Property Enumeration Order
              1. 6.9.1 The toString() Method
              2. 6.9.2 The toLocaleString() Method
              3. 6.9.3 The valueOf() Method
              4. 6.9.4 The toJSON() Method
              1. 6.10.1 Shorthand Properties
              2. 6.10.2 Computed Property Names
              3. 6.10.3 Symbols as Property Names
              4. 6.10.4 Spread Operator
              5. 6.10.5 Shorthand Methods
              6. 6.10.6 Property Getters and Setters
              1. 7.1 Creating Arrays
                1. 7.1.1 Array Literals
                2. 7.1.2 The Spread Operator
                3. 7.1.3 The Array() Constructor
                4. 7.1.4 Array.of()
                5. 7.1.5 Array.from()
                1. 7.8.1 Array Iterator Methods
                2. 7.8.2 Flattening arrays with flat() and flatMap()
                3. 7.8.3 Adding arrays with concat()
                4. 7.8.4 Stacks and Queues with push(), pop(), shift(), and unshift()
                5. 7.8.5 Subarrays with slice(), splice(), fill(), and copyWithin()
                6. 7.8.6 Array Searching and Sorting Methods
                7. 7.8.7 Array to String Conversions
                8. 7.8.8 Static Array Functions
                1. 8.1 Defining Functions
                  1. 8.1.1 Function Declarations
                  2. 8.1.2 Function Expressions
                  3. 8.1.3 Arrow Functions
                  4. 8.1.4 Nested Functions
                  1. 8.2.1 Function Invocation
                  2. 8.2.2 Method Invocation
                  3. 8.2.3 Constructor Invocation
                  4. 8.2.4 Indirect Invocation
                  5. 8.2.5 Implicit Function Invocation
                  1. 8.3.1 Optional Parameters and Defaults
                  2. 8.3.2 Rest Parameters and Variable-Length Argument Lists
                  3. 8.3.3 The Arguments Object
                  4. 8.3.4 The Spread Operator for Function Calls
                  5. 8.3.5 Destructuring Function Arguments into Parameters
                  6. 8.3.6 Argument Types
                  1. 8.4.1 Defining Your Own Function Properties
                  1. 8.7.1 The length Property
                  2. 8.7.2 The name Property
                  3. 8.7.3 The prototype Property
                  4. 8.7.4 The call() and apply() Methods
                  5. 8.7.5 The bind() Method
                  6. 8.7.6 The toString() Method
                  7. 8.7.7 The Function() Constructor
                  1. 8.8.1 Processing Arrays with Functions
                  2. 8.8.2 Higher-Order Functions
                  3. 8.8.3 Partial Application of Functions
                  4. 8.8.4 Memoization
                  1. 9.1 Classes and Prototypes
                  2. 9.2 Classes and Constructors
                    1. 9.2.1 Constructors, Class Identity, and instanceof
                    2. 9.2.2 The constructor Property
                    1. 9.3.1 Static Methods
                    2. 9.3.2 Getters, Setters, and other Method Forms
                    3. 9.3.3 Public, Private, and Static Fields
                    4. 9.3.4 Example: A Complex Number Class
                    1. 9.5.1 Subclasses and Prototypes
                    2. 9.5.2 Subclasses with extends and super
                    3. 9.5.3 Delegation Instead of Inheritance
                    4. 9.5.4 Class Hierarchies and Abstract Classes
                    1. 10.1 Modules with Classes, Objects, and Closures
                      1. 10.1.1 Automating Closure-Based Modularity
                      1. 10.2.1 Node Exports
                      2. 10.2.2 Node Imports
                      3. 10.2.3 Node-Style Modules on the Web
                      1. 10.3.1 ES6 Exports
                      2. 10.3.2 ES6 Imports
                      3. 10.3.3 Imports and Exports with Renaming
                      4. 10.3.4 Re-Exports
                      5. 10.3.5 JavaScript Modules on the Web
                      6. 10.3.6 Dynamic Imports with import()
                      7. 10.3.7 import.meta.url
                      1. 11.1 Sets and Maps
                        1. 11.1.1 The Set Class
                        2. 11.1.2 The Map Class
                        3. 11.1.3 WeakMap and WeakSet
                        1. 11.2.1 Typed Array Types
                        2. 11.2.2 Creating Typed Arrays
                        3. 11.2.3 Using Typed Arrays
                        4. 11.2.4 Typed Array Methods and Properties
                        5. 11.2.5 DataView and Endianness
                        1. 11.3.1 Defining Regular Expressions
                        2. 11.3.2 String Methods for Pattern Matching
                        3. 11.3.3 The RegExp Class
                        1. 11.4.1 Timestamps
                        2. 11.4.2 Date Arithmetic
                        3. 11.4.3 Formatting and Parsing Date Strings
                        1. 11.6.1 JSON Customizations
                        1. 11.7.1 Formatting Numbers
                        2. 11.7.2 Formatting Dates and Times
                        3. 11.7.3 Comparing Strings
                        1. 11.8.1 Formatted Output with Console
                        1. 11.9.1 Legacy URL Functions
                        1. 12.1 How Iterators Work
                        2. 12.2 Implementing Iterable Objects
                          1. 12.2.1 “Closing” an Iterator: The Return Method
                          1. 12.3.1 Generator Examples
                          2. 12.3.2 yield* and Recursive Generators
                          1. 12.4.1 The Return Value of a Generator Function
                          2. 12.4.2 The Value of a yield Expression
                          3. 12.4.3 The return() and throw() Methods of a Generator
                          4. 12.4.4 A Final Note About Generators
                          1. 13.1 Asynchronous Programming with Callbacks
                            1. 13.1.1 Timers
                            2. 13.1.2 Events
                            3. 13.1.3 Network Events
                            4. 13.1.4 Callbacks and Events in Node
                            1. 13.2.1 Using Promises
                            2. 13.2.2 Chaining Promises
                            3. 13.2.3 Resolving Promises
                            4. 13.2.4 More on Promises and Errors
                            5. 13.2.5 Promises in Parallel
                            6. 13.2.6 Making Promises
                            7. 13.2.7 Promises in Sequence
                            1. 13.3.1 await Expressions
                            2. 13.3.2 async Functions
                            3. 13.3.3 Awaiting Multiple Promises
                            4. 13.3.4 Implementation Details
                            1. 13.4.1 The for/await Loop
                            2. 13.4.2 Asynchronous Iterators
                            3. 13.4.3 Asynchronous Generators
                            4. 13.4.4 Implementing Asynchronous Iterators
                            1. 14.1 Property Attributes
                            2. 14.2 Object Extensibility
                            3. 14.3 The prototype Attribute
                            4. 14.4 Well-Known Symbols
                              1. 14.4.1 Symbol.iterator and Symbol.asyncIterator
                              2. 14.4.2 Symbol.hasInstance
                              3. 14.4.3 Symbol.toStringTag
                              4. 14.4.4 Symbol.species
                              5. 14.4.5 Symbol.isConcatSpreadable
                              6. 14.4.6 Pattern-Matching Symbols
                              7. 14.4.7 Symbol.toPrimitive
                              8. 14.4.8 Symbol.unscopables
                              1. 14.7.1 Proxy Invariants
                              1. 15.1 Web Programming Basics
                                1. 15.1.1 JavaScript in HTML Tags
                                2. 15.1.2 The Document Object Model
                                3. 15.1.3 The Global Object in Web Browsers
                                4. 15.1.4 Scripts Share a Namespace
                                5. 15.1.5 Execution of JavaScript Programs
                                6. 15.1.6 Program Input and Output
                                7. 15.1.7 Program Errors
                                8. 15.1.8 The Web Security Model
                                1. 15.2.1 Event Categories
                                2. 15.2.2 Registering Event Handlers
                                3. 15.2.3 Event Handler Invocation
                                4. 15.2.4 Event Propagation
                                5. 15.2.5 Event Cancellation
                                6. 15.2.6 Dispatching Custom Events
                                1. 15.3.1 Selecting Document Elements
                                2. 15.3.2 Document Structure and Traversal
                                3. 15.3.3 Attributes
                                4. 15.3.4 Element Content
                                5. 15.3.5 Creating, Inserting, and Deleting Nodes
                                6. 15.3.6 Example: Generating a Table of Contents
                                1. 15.4.1 CSS Classes
                                2. 15.4.2 Inline Styles
                                3. 15.4.3 Computed Styles
                                4. 15.4.4 Scripting Stylesheets
                                5. 15.4.5 CSS Animations and Events
                                1. 15.5.1 Document Coordinates and Viewport Coordinates
                                2. 15.5.2 Querying the Geometry of an Element
                                3. 15.5.3 Determining the Element at a Point
                                4. 15.5.4 Scrolling
                                5. 15.5.5 Viewport Size, Content Size, and Scroll Position
                                1. 15.6.1 Using Web Components
                                2. 15.6.2 HTML Templates
                                3. 15.6.3 Custom Elements
                                4. 15.6.4 Shadow DOM
                                5. 15.6.5 Example: a Web Component
                                1. 15.7.1 SVG in HTML
                                2. 15.7.2 Scripting SVG
                                3. 15.7.3 Creating SVG Images with JavaScript
                                1. 15.8.1 Paths and Polygons
                                2. 15.8.2 Canvas Dimensions and Coordinates
                                3. 15.8.3 Graphics Attributes
                                4. 15.8.4 Canvas Drawing Operations
                                5. 15.8.5 Coordinate System Transforms
                                6. 15.8.6 Clipping
                                7. 15.8.7 Pixel Manipulation
                                1. 15.9.1 The Audio() Constructor
                                2. 15.9.2 The WebAudio API
                                1. 15.10.1 Loading New Documents
                                2. 15.10.2 Browsing History
                                3. 15.10.3 History Management with hashchange Events
                                4. 15.10.4 History Management with pushState()
                                1. 15.11.1 fetch()
                                2. 15.11.2 Server-Sent Events
                                3. 15.11.3 WebSockets
                                1. 15.12.1 localStorage and sessionStorage
                                2. 15.12.2 Cookies
                                3. 15.12.3 IndexedDB
                                1. 15.13.1 Worker Objects
                                2. 15.13.2 The Global Object in Workers
                                3. 15.13.3 Importing Code into a Worker
                                4. 15.13.4 Worker Execution Model
                                5. 15.13.5 postMessage(), MessagePorts, and MessageChannels
                                6. 15.13.6 Cross-Origin Messaging with postMessage()
                                1. 15.15.1 HTML and CSS
                                2. 15.15.2 Performance
                                3. 15.15.3 Security
                                4. 15.15.4 WebAssembly
                                5. 15.15.5 More Document and Window Features
                                6. 15.15.6 Events
                                7. 15.15.7 Progressive Web Apps and Service Workers
                                8. 15.15.8 Mobile Device APIs
                                9. 15.15.9 Binary APIs
                                10. 15.15.10 Media APIs
                                11. 15.15.11 Cryptography and Related APIs
                                1. 16.1 Node Programming Basics
                                  1. 16.1.1 Console Output
                                  2. 16.1.2 Command-Line Arguments and Environment Variables
                                  3. 16.1.3 Program Life Cycle
                                  4. 16.1.4 Node Modules
                                  5. 16.1.5 The Node Package Manager
                                  1. 16.5.1 Pipes
                                  2. 16.5.2 Asynchronous Iteration
                                  3. 16.5.3 Writing to Streams and Handling Backpressure
                                  4. 16.5.4 Reading Streams with Events
                                  1. 16.7.1 Paths, File Descriptors, and FileHandles
                                  2. 16.7.2 Reading Files
                                  3. 16.7.3 Writing Files
                                  4. 16.7.4 File Operations
                                  5. 16.7.5 File Metadata
                                  6. 16.7.6 Working with Directories
                                  1. 16.10.1 execSync() and execFileSync()
                                  2. 16.10.2 exec() and execFile()
                                  3. 16.10.3 spawn()
                                  4. 16.10.4 fork()
                                  1. 16.11.1 Creating Workers and Passing Messages
                                  2. 16.11.2 The Worker Execution Environment
                                  3. 16.11.3 Communication Channels and MessagePorts
                                  4. 16.11.4 Transferring MessagePorts and Typed Arrays
                                  5. 16.11.5 Sharing Typed Arrays Between Threads
                                  1. 17.1 Linting with ESLint
                                  2. 17.2 JavaScript Formatting with Prettier
                                  3. 17.3 Unit Testing with Jest
                                  4. 17.4 Package Management with npm
                                  5. 17.5 Code Bundling
                                  6. 17.6 Transpilation with Babel
                                  7. 17.7 JSX: Markup Expressions in JavaScript
                                  8. 17.8 Type Checking with Flow
                                    1. 17.8.1 Installing and Running Flow
                                    2. 17.8.2 Using Type Annotations
                                    3. 17.8.3 Class Types
                                    4. 17.8.4 Object Types
                                    5. 17.8.5 Type Aliases
                                    6. 17.8.6 Array Types
                                    7. 17.8.7 Other Parameterized Types
                                    8. 17.8.8 Read-Only Types
                                    9. 17.8.9 Function Types
                                    10. 17.8.10 Union Types
                                    11. 17.8.11 Enumerated Types and Discriminated Unions
                                    Show and hide more

                                    Product information

                                    • Title: JavaScript: The Definitive Guide, 7th Edition
                                    • Author(s): David Flanagan
                                    • Release date: May 2020
                                    • Publisher(s): O'Reilly Media, Inc.
                                    • ISBN: 9781491952023