JavaScript: The Definitive Guide, 7th Edition

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.

Topics include:

  • Types, values, variables, expressions, operators, statements, objects, and arrays
  • Functions, classes, modules, iterators, generators, Promises, and async/await
  • JavaScript’s standard library: data structures, regular expressions, JSON, i18n, etc.
  • The web platform: documents, components, graphics, networking, storage, and threads
  • Node.js: buffers, files, streams, threads, child processes, web clients, and web servers
  • Tools and language extensions that professional JavaScript developers rely on

Publisher resources

View/Submit Errata

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
  2. Introduction to JavaScript
    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
  3. Lexical Structure
    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
    5. 2.5 Unicode
      1. 2.5.1 Unicode Escape Sequences
      2. 2.5.2 Unicode Normalization
    6. 2.6 Optional Semicolons
    7. 2.7 Summary
  4. Types, Values, and Variables
    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
    3. 3.3 Text
      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
    4. 3.4 Boolean Values
    5. 3.5 null and undefined
    6. 3.6 Symbols
    7. 3.7 The Global Object
    8. 3.8 Immutable Primitive Values and Mutable Object References
    9. 3.9 Type Conversions
      1. 3.9.1 Conversions and Equality
      2. 3.9.2 Explicit Conversions
      3. 3.9.3 Object to Primitive Conversions
    10. 3.10 Variable Declaration and Assignment
      1. 3.10.1 Declarations with let and const
      2. 3.10.2 Variable Declarations with var
      3. 3.10.3 Destructuring Assignment
    11. 3.11 Summary
  5. Expressions and Operators
    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
    5. 4.5 Invocation Expressions
      1. 4.5.1 Conditional Invocation
    6. 4.6 Object Creation Expressions
    7. 4.7 Operator Overview
      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
    8. 4.8 Arithmetic Expressions
      1. 4.8.1 The + Operator
      2. 4.8.2 Unary Arithmetic Operators
      3. 4.8.3 Bitwise Operators
    9. 4.9 Relational Expressions
      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
    10. 4.10 Logical Expressions
      1. 4.10.1 Logical AND (&&)
      2. 4.10.2 Logical OR (||)
      3. 4.10.3 Logical NOT (!)
    11. 4.11 Assignment Expressions
      1. 4.11.1 Assignment with Operation
    12. 4.12 Evaluation Expressions
      1. 4.12.1 eval()
      2. 4.12.2 Global eval()
      3. 4.12.3 Strict eval()
    13. 4.13 Miscellaneous Operators
      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 (,)
    14. 4.14 Summary
  6. Statements
    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
    4. 5.4 Loops
      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
    5. 5.5 Jumps
      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
    6. 5.6 Miscellaneous Statements
      1. 5.6.1 with
      2. 5.6.2 debugger
      3. 5.6.3 “use strict”
    7. 5.7 Declarations
      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
    8. 5.8 Summary of JavaScript Statements
  7. Objects
    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()
    3. 6.3 Querying and Setting Properties
      1. 6.3.1 Objects As Associative Arrays
      2. 6.3.2 Inheritance
      3. 6.3.3 Property Access Errors
    4. 6.4 Deleting Properties
    5. 6.5 Testing Properties
    6. 6.6 Enumerating Properties
      1. 6.6.1 Property Enumeration Order
    7. 6.7 Extending Objects
    8. 6.8 Serializing Objects
    9. 6.9 Object Methods
      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
    10. 6.10 Extended Object Literal Syntax
      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
    11. 6.11 Summary
  8. Arrays
    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()
    2. 7.2 Reading and Writing Array Elements
    3. 7.3 Sparse Arrays
    4. 7.4 Array Length
    5. 7.5 Adding and Deleting Array Elements
    6. 7.6 Iterating Arrays
    7. 7.7 Multidimensional Arrays
    8. 7.8 Array Methods
      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
    9. 7.9 Array-Like Objects
    10. 7.10 Strings as Arrays
    11. 7.11 Summary
  9. 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
    2. 8.2 Invoking 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
    3. 8.3 Function Arguments and Parameters
      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
    4. 8.4 Functions as Values
      1. 8.4.1 Defining Your Own Function Properties
    5. 8.5 Functions as Namespaces
    6. 8.6 Closures
    7. 8.7 Function Properties, Methods, and Constructor
      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
    8. 8.8 Functional Programming
      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
    9. 8.9 Summary
  10. Classes
    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
    3. 9.3 Classes with the class Keyword
      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
    4. 9.4 Adding Methods to Existing Classes
    5. 9.5 Subclasses
      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
    6. 9.6 Summary
  11. Modules
    1. 10.1 Modules with Classes, Objects, and Closures
      1. 10.1.1 Automating Closure-Based Modularity
    2. 10.2 Modules in Node
      1. 10.2.1 Node Exports
      2. 10.2.2 Node Imports
      3. 10.2.3 Node-Style Modules on the Web
    3. 10.3 Modules in ES6
      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
    4. 10.4 Summary
  12. The JavaScript Standard Library
    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
    2. 11.2 Typed Arrays and Binary Data
      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
    3. 11.3 Pattern Matching with Regular Expressions
      1. 11.3.1 Defining Regular Expressions
      2. 11.3.2 String Methods for Pattern Matching
      3. 11.3.3 The RegExp Class
    4. 11.4 Dates and Times
      1. 11.4.1 Timestamps
      2. 11.4.2 Date Arithmetic
      3. 11.4.3 Formatting and Parsing Date Strings
    5. 11.5 Error Classes
    6. 11.6 JSON Serialization and Parsing
      1. 11.6.1 JSON Customizations
    7. 11.7 The Internationalization API
      1. 11.7.1 Formatting Numbers
      2. 11.7.2 Formatting Dates and Times
      3. 11.7.3 Comparing Strings
    8. 11.8 The Console API
      1. 11.8.1 Formatted Output with Console
    9. 11.9 URL APIs
      1. 11.9.1 Legacy URL Functions
    10. 11.10 Timers
    11. 11.11 Summary
  13. Iterators and Generators
    1. 12.1 How Iterators Work
    2. 12.2 Implementing Iterable Objects
      1. 12.2.1 “Closing” an Iterator: The Return Method
    3. 12.3 Generators
      1. 12.3.1 Generator Examples
      2. 12.3.2 yield* and Recursive Generators
    4. 12.4 Advanced Generator Features
      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
    5. 12.5 Summary
  14. Asynchronous JavaScript
    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
    2. 13.2 Promises
      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
    3. 13.3 async and await
      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
    4. 13.4 Asynchronous Iteration
      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
    5. 13.5 Summary
  15. Metaprogramming
    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
    5. 14.5 Template Tags
    6. 14.6 The Reflect API
    7. 14.7 Proxy Objects
      1. 14.7.1 Proxy Invariants
    8. 14.8 Summary
  16. JavaScript in Web Browsers
    1. 15.1 Web Programming Basics
      1. 15.1.1 JavaScript in HTML <script> 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
    2. 15.2 Events
      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
    3. 15.3 Scripting Documents
      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
    4. 15.4 Scripting CSS
      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
    5. 15.5 Document Geometry and Scrolling
      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
    6. 15.6 Web Components
      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 <search-box> Web Component
    7. 15.7 SVG: Scalable Vector Graphics
      1. 15.7.1 SVG in HTML
      2. 15.7.2 Scripting SVG
      3. 15.7.3 Creating SVG Images with JavaScript
    8. 15.8 Graphics in a <canvas>
      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
    9. 15.9 Audio APIs
      1. 15.9.1 The Audio() Constructor
      2. 15.9.2 The WebAudio API
    10. 15.10 Location, Navigation, and History
      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()
    11. 15.11 Networking
      1. 15.11.1 fetch()
      2. 15.11.2 Server-Sent Events
      3. 15.11.3 WebSockets
    12. 15.12 Storage
      1. 15.12.1 localStorage and sessionStorage
      2. 15.12.2 Cookies
      3. 15.12.3 IndexedDB
    13. 15.13 Worker Threads and Messaging
      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()
    14. 15.14 Example: The Mandelbrot Set
    15. 15.15 Summary and Suggestions for Further Reading
      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
  17. Server-Side JavaScript with Node
    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
    2. 16.2 Node Is Asynchronous by Default
    3. 16.3 Buffers
    4. 16.4 Events and EventEmitter
    5. 16.5 Streams
      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
    6. 16.6 Process, CPU, and Operating System Details
    7. 16.7 Working with Files
      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
    8. 16.8 HTTP Clients and Servers
    9. 16.9 Non-HTTP Network Servers and Clients
    10. 16.10 Working with Child Processes
      1. 16.10.1 execSync() and execFileSync()
      2. 16.10.2 exec() and execFile()
      3. 16.10.3 spawn()
      4. 16.10.4 fork()
    11. 16.11 Worker Threads
      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
    12. 16.12 Summary
  18. JavaScript Tools and Extensions
    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
    9. 17.9 Summary
  19. Index

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