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
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
7. Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....155
7.1 Creating Arrays....156
7.2 Reading and Writing Array Elements....159
7.3 Sparse Arrays....160
7.4 Array Length....161
7.5 Adding and Deleting Array Elements....161
7.6 Iterating Arrays....162
7.7 Multidimensional Arrays....164
7.8 Array Methods....165
7.9 Array-Like Objects....177
7.10 Strings as Arrays....179
7.11 Summary....180
8. Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....181
8.1 Defining Functions....182
8.2 Invoking Functions....186
8.3 Function Arguments and Parameters....193
8.4 Functions as Values....200
8.5 Functions as Namespaces....203
8.6 Closures....204
8.7 Function Properties, Methods, and Constructor....209
8.8 Functional Programming....213
8.9 Summary....219
9. Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....221
9.1 Classes and Prototypes....222
9.2 Classes and Constructors....224
9.3 Classes with the class Keyword....229
9.4 Adding Methods to Existing Classes....236
9.5 Subclasses....237
9.6 Summary....248
10. Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....249
10.1 Modules with Classes, Objects, and Closures....250
10.2 Modules in Node....253
10.3 Modules in ES6....255
10.4 Summary....266
11. The JavaScript Standard Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....267
11.1 Sets and Maps....268
11.2 Typed Arrays and Binary Data....275
11.3 Pattern Matching with Regular Expressions....281
11.4 Dates and Times....300
11.5 Error Classes....304
11.6 JSON Serialization and Parsing....306
11.7 The Internationalization API....309
11.8 The Console API....317
11.9 URL APIs....320
11.10 Timers....323
11.11 Summary....325
12. Iterators and Generators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....327
12.1 How Iterators Work....328
12.2 Implementing Iterable Objects....329
12.3 Generators....332
12.4 Advanced Generator Features....336
12.5 Summary....339
13. Asynchronous JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....341
13.1 Asynchronous Programming with Callbacks....342
13.2 Promises....346
13.3 async and await....367
13.4 Asynchronous Iteration....370
13.5 Summary....377
14. Metaprogramming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....379
14.1 Property Attributes....380
14.2 Object Extensibility....384
14.3 The prototype Attribute....386
14.4 Well-Known Symbols....387
14.5 Template Tags....395
14.6 The Reflect API....397
14.7 Proxy Objects....399
14.8 Summary....406
15. JavaScript in Web Browsers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....409
15.1 Web Programming Basics....411
15.2 Events....426
15.3 Scripting Documents....437
15.4 Scripting CSS....452
15.5 Document Geometry and Scrolling....459
15.6 Web Components....464
15.7 SVG: Scalable Vector Graphics....477
15.8 Graphics in a <canvas>....484
15.9 Audio APIs....507
15.10 Location, Navigation, and History....509
15.11 Networking....518
15.12 Storage....536
15.13 Worker Threads and Messaging....548
15.14 Example: The Mandelbrot Set....555
15.15 Summary and Suggestions for Further Reading....568
16. Server-Side JavaScript with Node. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....577
16.1 Node Programming Basics....578
16.2 Node Is Asynchronous by Default....583
16.3 Buffers....586
16.4 Events and EventEmitter....588
16.5 Streams....590
16.6 Process, CPU, and Operating System Details....601
16.7 Working with Files....602
16.8 HTTP Clients and Servers....613
16.9 Non-HTTP Network Servers and Clients....617
16.10 Working with Child Processes....620
16.11 Worker Threads....625
16.12 Summary....634
17. JavaScript Tools and Extensions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....635
17.1 Linting with ESLint....636
17.2 JavaScript Formatting with Prettier....637
17.3 Unit Testing with Jest....638
17.4 Package Management with npm....640
17.5 Code Bundling....642
17.6 Transpilation with Babel....644
17.7 JSX: Markup Expressions in JavaScript....645
17.8 Type Checking with Flow....649
17.9 Summary....665
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....667
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.
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