You Don't Know JS.... Part 2

· by Dave LeBlanc · Read in about 2 min · (383 words)

Continuing an Open Source book series on the deepest parts of JavaScript. Part 2: Scope and Closures

This is part 2 of a series of posts based on the wonderful series of books written by Kyle Simpson and available over on GitHub. Yo can check out part 1 here.

After the great introduction and overview of JavaScript in the first book I was excited to dive deeper into the innards of the language and this book doesn’t disappoint. «Scope and Closures» dives headfirst into basic compiler theory helping the reader understand the relationship between the engine, compiler and scope. Don’t let this scare you however because the author does a great job of explaining with great detail in an accessible fashion using a few funny metaphors and comparisons along the way.

JavaScript uses lexical scope and this book gives us thorough insight into what that means and how it works. The author uses a lot of code examples to demonstrate the differences in various scenarios and continues to reinforce those examples with the details we learned about the compiler earlier on in the book.

Hoisting is another important topic discussed in this book. While hoisting isn’t an overly complicated concept it is one that many JavaScript developers don’t seem to fully understand and the chapter in this book does a wonderful job of explaining it in a simple enough fashion

Lastly the book goes on to discuss closures, described by the author as an incredibly important, but persistently elusive, almost mythological, part of the language. The author takes you by the hand and leads you down a path to enlightenment about closures. Once you get there you begin to realize that not only are closures not something to be intimidated by but that they are something that is likely all around you in JavaScript without ever realizing it.

The book has a few interesting Appendixes that touch on dynamic scope to give you some comparison to JavaScript’s lexical scope as well as some interesting patterns that can be used to polyfill block scoping features into pre ES6 JavaScript.

At the end of the day this book is a great continuation of the series and I definitely look forward to continue my adventure down the JavaScript rabbit hole with the rest of the series.