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 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 Introduction PART I: LANGUAGE Chapter 1: Values, Types, and Operators Chapter 2: Program Structure Chapter 3: Functions Chapter 4: Data Structures: Objects and Arrays Chapter 5: Higher-Order Functions Chapter 6: The Secret Life of Objects Chapter 7: Project: A Robot Chapter 8: Bugs and Errors. Chapter 9: Regular Expressions Chapter 10: Modules Chapter 11: Asynchronous Programming Chapter 12: Project: A Programming Language PART II: BROWSER Chapter 13: JavaScript and the Browser Chapter 14: The Document Object Model Chapter 15: Handling Events Chapter 16: Project: A Platform Game Chapter 17: Drawing on Canvas Chapter 18: HTTP and Forms Chapter 19: Project: A Pixel Art Editor PART III: NODE Chapter 20: Node.js Chapter 21: Project: Skill-Sharing Website Chapter 22: JavaScript and Performance Exercise Hints Index "*Eloquent JavaScript* is a book providing an introduction to the JavaScript programming language and programming in general."