You Don’t Know JS: Scopes & Closures

Hello people, Welcome to the next post of TIL JS. In the last post we discussed about how important it is to read technical books and we learnt about the JavaScript: Good parts book. Today we will learn about another book ‘You Don’t know JS’ by Kyle Simpson. This is a series of books containing 7 titles. One of the books in this series is ‘Scopes & Closures’ and we are going to look about it here.

Before going into this, this is an open source book and it is available in github. You can read it from here: https://github.com/getify/You-Dont-Know-JS

The good thing about this series of books is, the author has clearly segregated the topics in JavaScript and explained those topics clearly and deeply. Also the author explains about ES6 features related to the core concept that he is discussing. I find this very helpful, because most of the famous technical books in JavaScript deal only ES3 or ES5. Though you can start from any of the books in the series, I started with scopes and closures because I felt this is the basic building block upon which much more complex topics can be constructed.

What is Scope?

The book consists of 5 chapters and the first chapter begins with the basic question of ‘What is scope?’. The author begins with discussing about the compiler theory and the meaning of scope and how the compiler interprets the scope. This is really good as no other JavaScript book or article I have read has explained the scope starting with compiler theory. The chapter slowly moves on to explain a bit about the JavaScript engine and how the engine computes the scope. Instead of a boring text, the author explains the concept by having an imaginary conversation between the JavaScript Engine and the Scope. This makes us to understand the topic more clearly.

Lexical Scope

There are two types of scope, one is the lexical scope and the another one is dynamic scope. He explains the difference between these two and begins to explain about the lexical scope in Chapter 2. Because JavaScript is a lexical scope based language. Then he explains about how we can cheat the lexical scope in JavaScript (hint: ‘eval’ and ‘with’) and why that should be avoided in our code at any cost.

Function Scope and Block Scope

The third chapter deals about the function scope and block scope (ES6). The author explains how to create a scope using functions. He continues to explain about the different ways a function can be declared in JavaScript and how each declaration affects the scope. The chapter ends with block scopes in JavaScript using let and const keywords.

Hoisting

The fourth chapter explains about the concept of hoisting in JavaScript and the rules behind hoisting. The chapter deals exclusively about how the variables and functions are hoisted in JavaScript and how the declaration affects the scope.

Closures & Modules

The fifth and final chapter explains the link between scopes and closures in JavaScript. The author explains about how to create a Closure in JavaScript and the commonly occurring errors while creating a closure. Though most of the JavaScript books stop at this point, he continues on how the ES6 affects the closures and the significance of ES6 modules.

Apart from these chapters, the book also has 4 Appendix. Remember the lexical scope and dynamic scope we discussed at the beginning and JavaScript is lexical scope based? In the appendix the author explains about dynamic scope also. These sort of things makes this book stand apart from other similar books.

For those who are entering into JavaScript, I would suggest to begin with this book. There is a famous quote which says,

If you can’t explain it simple enough, then you don’t understand.

If you are an experienced developer and if you can’t explain the concepts of scope and closure to others simply, then this is a must read for you. I would like to thank the author Kyle Simpson for providing such a quality content as an open source material. (You can still buy hard copy book from Amazon from here).

I have completed just 2 books in this series. Will review the ‘This & Object prototypes’ book in the next blog post.

Have you read this book? Want to read? Let me know about your thoughts in the comments.

See you in the next post, until then keep learning! :)

Like what you read? Give Siva Subramaniam a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.