Start building beautiful web applications in JavaScript with the bestselling introduction to the language, updated with new features, fresh exercises, and fun projects. JavaScript, the programming language that allows us to add programs to web pages, lies at the heart of almost every modern web application, from social media sites to browser-based games. Just about every device can run it, making it a great choice for writing universally useful code. The fourth edition of this classic textbook takes you on a journey through the language of the web, starting from its basic elements and building up to engaging, complete programs. The author’s personal experiences from years of maintaining popular open source projects enliven the text with practical insights and examples. This edition of Eloquent JavaScript updates the book to describe the 2024 version of JavaScript, and shifts the attention given to some topics to better reflect current development practices. The text takes a practical approach to teaching, rooting theory in plenty of motivating examples. The first half of the book describes the fundamentals of the language, whereas the second half shows how to apply it in two programming environments: the web browser and Node.js. Five project chapters show how to build bigger programs, working through these projects in a piecemeal, conversational way. They include a pathfinding robot, a small programming language, a platform game, a pixel drawing program, and a simple dynamic website. Exercises provided at the end of most chapters will challenge you to creatively apply the concepts and techniques introduced. The book’s companion website provides an interactive environment for working on these exercises and playing with the example programs. Whether you’re looking to learn JavaScript or to deepen your programming skills, you’ll find this book full of stimulating material. Updated to ECMAScript 2024 Introduction On programming Why language matters What is JavaScript? Code, and what to do with it Overview of this book Typographic conventions Values, Types, and Operators Values Numbers Strings Unary operators Boolean values Empty values Automatic type conversion Summary Program Structure Expressions and statements Bindings Binding names The environment Functions The console.log function Return values Control flow Conditional execution while and do loops Indenting Code for loops Breaking Out of a Loop Updating bindings succinctly Dispatching on a value with switch Capitalization Comments Summary Exercises Functions Defining a function Bindings and scopes Nested scope Functions as values Declaration notation Arrow functions The call stack Optional Arguments Closure Recursion Growing functions Functions and side effects Summary Exercises Data Structures: Objects and Arrays The weresquirrel Datasets Properties Methods Objects Mutability The lycanthrope's log Computing correlation Array loops The final analysis Further arrayology Strings and their properties Rest parameters The Math object Destructuring Optional property access JSON Summary Exercises Higher-Order Functions Abstraction Abstracting repetition Higher-order functions Script dataset Filtering arrays Transforming with map Summarizing with reduce Composability Strings and character codes Recognizing text Summary Exercises The Secret Life of Objects Abstract Data Types Methods Prototypes Classes Private Properties Overriding derived properties Maps Polymorphism Getters, setters, and statics Symbols The iterator interface Inheritance The instanceof operator Summary Exercises Project: A Robot Meadowfield The task Persistent data Simulation The mail truck's route Pathfinding Exercises Bugs and Errors Language Strict mode Types Testing Debugging Error propagation Exceptions Cleaning up after exceptions Selective catching Assertions Summary Exercises Regular Expressions Creating a regular expression Testing for matches Sets of characters International characters Repeating parts of a pattern Grouping subexpressions Matches and groups The Date class Boundaries and look-ahead Choice patterns The mechanics of matching Backtracking The replace method Greed Dynamically creating RegExp objects The search method The lastIndex property Parsing an INI file Code units and characters Summary Exercises Modules Modular programs ES modules Packages CommonJS modules Building and bundling Module design Summary Exercises Asynchronous Programming Asynchronicity Callbacks Promises Failure Carla Breaking In Async functions Generators A Corvid Art Project The event loop Asynchronous bugs Summary Exercises Project: A Programming Language Parsing The evaluator Special forms The environment Functions Compilation Cheating Exercises JavaScript and the Browser Networks and the Internet The Web HTML HTML and JavaScript In the sandbox Compatibility and the browser wars The Document Object Model Document structure Trees The standard Moving through the tree Finding elements Changing the document Creating nodes Attributes Layout Styling Cascading styles Query selectors Positioning and animating Summary Exercises Handling Events Event handlers Events and DOM nodes Event objects Propagation Default actions Key events Pointer events Scroll events Focus events Load event Events and the event loop Timers Debouncing Summary Exercises Project: A Platform Game The game The technology Levels Reading a level Actors Drawing Motion and collision Actor updates Tracking keys Running the game Exercises Drawing on Canvas SVG The canvas element Lines and surfaces Paths Curves Drawing a pie chart Text Images Transformation Storing and clearing transformations Back to the game Choosing a graphics interface Summary Exercises HTTP and Forms The protocol Browsers and HTTP Fetch HTTP sandboxing Appreciating HTTP Security and HTTPS Form fields Focus Disabled fields The form as a whole Text fields Checkboxes and radio buttons Select fields File fields Storing data client-side Summary Exercises Project: A Pixel Art Editor Components The state DOM building The canvas The application Drawing tools Saving and loading Undo history Let's draw Why is this so hard? Exercises Node.js Background The node command Modules Installing with NPM The filesystem module The HTTP module Streams A file server Summary Exercises Project: Skill-Sharing Website Design Long polling HTTP interface The server The client Exercises Exercise Hints Program Structure Functions Data Structures: Objects and Arrays Higher-Order Functions The Secret Life of Objects Project: A Robot Bugs and Errors Regular Expressions Modules Asynchronous Programming Project: A Programming Language The Document Object Model Handling Events Project: A Platform Game Drawing on Canvas HTTP and Forms Project: A Pixel Art Editor Node.js Project: Skill-Sharing Website 1 2 3 5 6 7 8 Part I: Language 11 12 12 13 14 14 15 16 16 17 18 18 19 20 23 23 24 26 26 26 27 27 28 28 30 31 32 33 33 34 35 35 36 36 36 37 37 39 39 40 41 42 42 43 44 45 46 47 50 52 52 53 53 53 53 57 58 58 59 60 60 63 64 65 67 67 69 70 72 73 74 75 75 76 77 77 77 77 78 81 82 82 84 85 86 86 87 88 89 91 92 92 92 93 93 93 95 95 96 97 98 100 101 102 104 105 106 107 109 110 111 111 111 112 112 115 115 117 118 119 121 121 123 123 124 124 127 127 128 129 130 131 132 133 134 136 138 138 139 139 139 141 141 142 142 144 145 146 146 147 148 149 149 150 151 153 154 154 155 156 158 159 160 160 161 161 163 164 164 166 167 170 170 172 173 173 173 173 175 176 177 178 180 181 182 184 185 186 189 190 191 191 191 192 192 195 195 199 200 202 203 204 205 205 205 206 206 206 Part II: Browser 209 209 211 211 xvi Contents in Detail 213 214 215 217 217 218 219 220 221 222 223 224 225 227 228 229 230 232 232 232 233 233 235 235 236 237 237 239 239 240 241 242 243 244 245 246 246 247 248 249 249 249 249 250 253 253 254 255 255 257 260 265 267 269 270 272 272 272 272 275 276 276 277 278 279 282 283 283 285 287 288 293 294 294 294 295 295 295 297 297 299 300 302 302 303 303 305 306 306 307 309 310 311 312 314 315 315 315 315 319 320 321 322 323 325 327 330 332 333 334 335 335 335 336 336 Part III: Node 339 339 340 341 342 342 343 344 345 346 347 352 353 353 353 353 355 356 356 357 359 359 360 362 364 365 365 366 367 369 370 371 371 371 373 374 374 376 377 380 381 383 384 385 386 386 386 387 409