Jump to ratings and reviews
Rate this book

You Don't Know JS #2

You Don't Know JS: Scope and Closures

Rate this book
No matter how much experience you have with JavaScript, odds are you don't fully understand the language. This concise yet in-depth guide takes you inside scope and closures, two core concepts you need to know to become a more efficient and effective JavaScript programmer. You'll learn how and why they work, and how an understanding of closures can be a powerful part of your development skillset.

Like other books in the You Don't Know JS series, Scope and Closures dives into trickier parts of the language that many JavaScript programmers simply avoid. Armed with this knowledge, you can achieve true JavaScript mastery.


Learn about scope, a set of rules to help JavaScript engines locate variables in your code
Go deeper into nested scope, a series of containers for variables and functions
Explore function- and block-based scope, "hoisting", and the patterns and benefits of scope-based hiding
Discover how to use closures for synchronous and asynchronous tasks, including the creation of JavaScript libraries

96 pages, Paperback

First published January 1, 2014

275 people are currently reading
1513 people want to read

About the author

Kyle Simpson

37 books360 followers
Kyle Simpson is an Open Web Evangelist from Austin, TX, who's passionate about all things JavaScript. He's an author, workshop trainer, tech speaker, and OSS contributor/leader.

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
1,166 (62%)
4 stars
595 (31%)
3 stars
101 (5%)
2 stars
12 (<1%)
1 star
4 (<1%)
Displaying 1 - 30 of 132 reviews
Profile Image for Sallar.
38 reviews40 followers
June 24, 2015
Does a programming book get better than this? I don't think so.
Profile Image for Dave Peticolas.
1,377 reviews45 followers
November 27, 2015
What a great little book. This might be the first Javascript book I've managed to finish without having my eyes glaze over from the tedium. Modern technical books tend to lean far too much on how-to examples and not enough time on deeper explanations. This book focuses on one particular aspect of the language, scope rules and closures, and explains it in depth. The author understands how important well-crafted analogies and mental models are for achieving genuine understanding.
Profile Image for Ilya Ivanov.
25 reviews9 followers
September 5, 2016
Marvelous book. Kyle deserves every cent received at Kickstarter.

The book is so concise, that it seems like every word is brushed until it shines, revealing the underlying truth about JavaScript and it's key differences from other languages.

Topics covered: scope, different types of scope, modules, IFFE, hoisting, closures, polyfilling. Code samples are short, intention revealing and very informative.

I've been through two books in this series and very thrilled to dive deeper into next books.

Thank you, Kyle, for showing us, that we might not understand the language we think we are and we just might need to spend some extra effort in order to effectively apply JavaScript.
Profile Image for Temo Tchanukvadze.
54 reviews8 followers
October 14, 2018
Recommended! It doesn't matter how good/bad JS developer you are. You need to fill your gaps and YDKJS is the best choice.
Profile Image for Tareef Mando.
134 reviews367 followers
July 2, 2018
الكتاب الثاني من سلسلة أنت لا تعرف جافاسكربت
إن كنتَ تبحث عن كتاب تتعلم من خلاله جافاسكربت يمكنك قراءة مراجعتي لكتاب هيدفيرست هنا *

كما يبدو واضحًا من عنوانه يتناول هذا الكتاب مفهومي النطاق ودالات الإغلاق - إن صحّت الترجمة - بشكل مُعمق ومُثقف، مستندًا بالشرح
بشكل أساسي إلى الطريقة التي يعمل بها مترجم اللغة compiler ** والقواعد التي يتبعها في معرفة أين وكيف يمكن الوصول إلى المتغيرات والدوال التي يتم تعريفها.

يعرض المؤلف كذلك بعض الطرق التي تشيع بين مبرمجي جافاسكربت كنوع من خداع القواعد التي يتبعها المترجم في بناء النطاقات، مثل دوال
eval & with
ويعرض لمساوئ استخدام هاتين الدالتين وبدائلهما

نصف الكتاب الثاني مخصص لعرض مفهومي Hoisting & Closure
وهي مفاهيم مفتاحية في فهم جافاسكربت بشكل أعمق.

أعجبتني التفاصيل والملاحظات الدقيقة التي يذكرها المؤلف على امتداد الكتاب، والتي تجد معظم المواقع التعليمية وكورسات الفيديو تجملها كثيرًا أو تتجاهلها ببساطة.
ولكن لم أحب أسلوبه الشخصي عند تهويل نقطة ما، هذا الأسلوب كان يستخدمه بعض الأساتذة في المدرسة
فمثلا قبل شرح مفهوم ما، يقول لك هذا المفهوم صعب جدا، وفهمه يعتبر أمر قليل بين المبرمجين ولكن يجب أن تفهمه وأن تحاول
حتى تصل إلى لحظة التنوير التي تدرك بها عن ماذا نتحدث تماما!
في المقابل وفي كتاب هيدفيرست، المؤلف كان يستدرج القارئ تدريجيا وبعد أن يفرغ من شرح مفهوم الكلوجر يقول له ما تعملته الآن يسمى كذا
وذلك لتجنيب القارئ أي حاجز نفسي عن ادارك مفهوم ما لمجرد أن هالة تصعيبية تنتشر عنه على الشبكة.

* https://www.goodreads.com/review/show...
** سواء كنت تُشغّل جافاسكربت في بيئة متصفح أو على سيرفر فإنه يجري ترجمة الكود إلى لغة الآلة قبل تنفيذه
Profile Image for Iwona.
495 reviews28 followers
September 9, 2018
Bardzo fajna książką skupiona na konkretnych funkcjonalnościach JavaScript jakimi są zakresy oraz domknięcia. Ich dobra znajomość jest o tyle ważna, iż pozwoli nam napisać lepszy, wydajniejszy kod a także uniknąć różnego rodzaju błędów związanych z nie do końca oczywistym działaniem JavaScript. Tym bardziej jeśli jesteśmy programistami jakiegoś silnie typowanego języka typu C# czy Java w którym wiele rzeczy działa nieco inaczej. Gdy zdążyliśmy się do nich przyzwyczaić zderzenie z JavaScriptem może być momentami bolesne. Książka ma na celu wytłumaczyć nam ten nieco inny punkt widzenia byśmy mogli poruszać się po tym języku sprawnie i pisać zrozumiały kod. Polecam i zaraz zapewne sięgnę po inne książki tej serii.
Profile Image for Irakli Iremashvili.
1 review
March 9, 2019
For most newcomers to Javascript (whether they know any other programming language or not), it's scope and closure behaviour may seem very strange. This book helps you understand this topic and become a better developer
Profile Image for Mark.
143 reviews6 followers
April 4, 2018
Not a bad deep dive into scope & closures. I picked up a good amount along the way. I'd definitely supplement this with YouTube videos to follow up.
Profile Image for Zvart.
30 reviews4 followers
June 3, 2020
Լիքը-լքիը խորանում ա բոլոր թեմաների մեջ , օրինակներն էլ շատ լավն են, ինչ խոսք շատ հեշտ չի կարդացվում, բայց հաստատ արժի կարդալ։
8 reviews
July 26, 2015
Awesome book. Helped me gain a better understanding of Scope and Closures and clear up some misconceptions! Before reading this book i actually thought JavaScript is an interpreted language but learned it is in fact a compiled language but its different from other compiled languages because compilation does not happen well in advance rather just milliseconds before execution. I also liked his definition of a closure, 'Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.'

Kyle has strong opinions but he backs them up, whether you agree with him or not you will learn the 'why' behind some of the tricky parts in JavaScript. Looking forward to other books in the series.
Profile Image for Dan.
Author 3 books9 followers
June 4, 2015
I needed something to explain scopes and closures to a Junior Dev I've started working with and mentoring recently. The usual examples and code samples weren't clicking, and I began to realize that, even for me, those old stand-by's weren't what actually got me to grock closure. I had to use the module pattern extensively before I really understood the concept at an intuitive level.

This book fills in what I needed experience to understand. It is concise, it is accurate, it is clear. You can't ask for better.
Profile Image for Prakash.
165 reviews96 followers
December 3, 2017
Perhaps not worthy of a stand-alone title. And don't really agree with all of the advices regarding let.

The author's advice is also contradictory in the spirit of the series which is to understand deeply and then use it correctly.

On the other hand, for let the recommended use is only for block scope and even then make it explicit with braces. Ahem, that's why you use the new keyword 'let'.

If your code breaks after replacing var with let, you weren't writing very clean code in the first place.
Profile Image for Joshua Isaac.
28 reviews2 followers
March 28, 2015
This book was a nice refresher of things I already knew, as well as a tidy introduction to some new syntax in EC6. I appreciated his coverage of IIFEs because that's syntax I'm always very confused about. This book was explicit in its scope (hah!) - it really didn't touch on much else besides scopes and closures - but it also didn't shy away from telling you about powerful language functions just because they are not nice to think about.
Profile Image for Robson Castilho.
266 reviews32 followers
December 16, 2016
Concepts such as lexical scopes, function and block scopes, closures, module pattern, IIFE and hoisting are all well explained with simple code examples. At the start, a very good intro about Javascript compilation process, its compiler and engine.
Additionally, some new ES6 features are presented in conjuction with pollyfill solutions for block scopes.
A very good book for those who wants to understand how JS works!
Profile Image for Jeff Richardson.
2 reviews
August 10, 2016
While I already considered myself somewhat of an expert when it came to scope and closure, I've read and heard Crockford talk about them a lot over the years, and Zakas books have a wealth of information on scope and closures as well I feel this book provided a new light on them. I'm a better Client Side Engineer for having read this book.
Profile Image for Hưng Đặng.
129 reviews71 followers
September 11, 2018
It is surprising that this good book is not too much beyond my comprehension of the language and my meager experience in the field is also a great help. Few things that I might need to go over again because I am still confused with them:

1. JS is compiled, it is not just interpreted

2. Tokenizing and lexing.

3. JS conpilation doens't work in a build step ahead of time like other language. So I guess it works in run time!

4. LHS cares about target of the assignment while RHS wants to know about the source of the assignments.

5. Function declaration is not a LHS look-up?

6. We have eval() and with that can mess up lexical scope. Then what the point of having them?

7. The Principle of Least Privilege (or Least Authority or Least Exposure)

Some amazing things that I have learnt: block-scoping, IIFE, carefully use of functions inside loops, MODULE, lexical scopes vs dynamics scope. I also know about so cool Revealing Module Pattern which is commonly used to build public API.
Profile Image for Md. Jamal Uddin.
81 reviews14 followers
September 30, 2018
Engine: Hey Scope, I have an RHS reference for foo. Ever heard of it?

Scope: Why yes, I have. Compiler declared it just a second ago. He's a function. Here you go.

Engine: Great, thanks! OK, I'm executing foo.

Engine: Hey, Scope, I've got an LHS reference for a, ever heard of it?

Scope: Why yes, I have. Compiler declared it as a formal parameter to foo just recently. Here you go.

Engine: Helpful as always, Scope. Thanks again. Now, time to assign 2 to a.

Engine: Hey, Scope, sorry to bother you again. I need an RHS look-up for console. Ever heard of it?

Scope: No problem, Engine, this is what I do all day. Yes, I've got console. He's built-in. Here ya go.

Engine: Perfect. Looking up log(..). OK, great, it's a function.

Engine: Yo, Scope. Can you help me out with an RHS reference to a. I think I remember it, but just want to double-check.

Scope: You're right, Engine. Same guy, hasn't changed. Here ya go.

Engine: Cool. Passing the value of a, which is 2, into log(..).
Profile Image for dantelk.
206 reviews20 followers
January 1, 2025
I've been writing Angular for a few years now, and I took lots of online trainings on it. I also learned a lot about TS. But never I spent any time understanding the core concepts of JS. Somehow I was receiving some clues about my ignorance on JS, but I was just keeping a blind eye on them, and continuing my learnings on more "fun" subjects, core Angular, rxjs etc etc.

Then this book appeared.

People say they don't like JS, and after reading this book, I know why!! Oh boy, I had to re-read this book twice, supplementing the ideas with more sources from the youtube (ColorCode was a good youtube channel at this point).

It is great this book is written, and it is terrible it had to be written. Now although I understand Scope and Closures, I don't like them at all, and I suspect at any job interview, the interviewer will be waiting with some "gotcha" questions on them to trap me!
Profile Image for Mark.
519 reviews82 followers
July 18, 2017
Excellent coverage over these topics. One part I appreciated so much was that these topics are rarely covered well in any learning book, but are covered very well here, with reference to other ways for a more complete understanding. Kyle tells us about dynamic scoping and highlights the potential pitfalls of an area that is a mixture ("this" which in one context operates with lexical scope and in another case operates with dynamic scope). For me, the real attraction was a good understanding of closure and how it could be used. Kyle also gave a great basic understanding of closure, then gave examples that expanded the understanding into ways one might use it. The only improvement that would have helped me is practical examples in real use. But that doesn't detract from this excellent coverage. I look forward to his next book.
Profile Image for David.
44 reviews4 followers
September 6, 2017
Did you know JavaScript is actually a compiled language? You probably did not. This and many other mind-blowing discoveries is what you will find after reading this book.

I learned what I know about JavaScript the usual way: try something; see it failing; try something else; see it working, but not really understanding why; move on with life.

This book enlightens you with the inner workings of JavaScript, specifically with something you use at every single JavaScript snippet you write, scope. It also talks in depth about closures, a usually obscure feature of JavaScript that no one seems to understand altogether.

If you, by any mean, and at any level, have to write JavaScript for your work or side-project, this series is a must.
Profile Image for Yanjia Li.
38 reviews3 followers
August 6, 2018
Another must read for Javascript developers who want to understand the mechanism behind the scene. Unlike Types & Grammar which somewhat looks tricky, the knowledge in this book could really help in work. This is one of the few books that explains what is closure and why it's called closure. To simply put, a closure close a scope. But maybe most of JS developers don't even understand lexical scope much, which prevent them from understanding closures more thoroughly. And the author caught that precisely.

One nit is that if Kyle could explain more about ES6 modules, although he did great for the old ways.
Profile Image for Devin.
405 reviews
May 14, 2018
I've puzzled through enough JavaScript to develop an intuitive sense of scope and closure. But the temptation is to never give it much thought in the name of churning out code that works. The reality is that "What is Closure?" is an incredibly common interview question for almost any tech screen and it pays to develop a vocabulary for discussing it.

The additional study of how to leverage an understanding of closure in one's code is invaluable. And clearly about to lead into a long discussion of "this" in the next volume.
Profile Image for Dawn Saquin.
33 reviews
July 26, 2018
Great book that helped me understand a little more about function and block scoping, and how to point out closures. I also got a clearer picture on when to use the variable declarations let and const.
I still have a few uncertainties about using 'this' and arrow functions together, since Kyle says to avoid it and stick to one. I guess I'll know more about it in the next book in the series. : )

Overall, this book helps me think more like a JS programmer and to know that functions are powerful in and out of their scope.
Profile Image for Kevin Sweet.
68 reviews1 follower
August 25, 2019
This installment in YDKJS focused too much on the parts of JS that you're probably not using anymore. Yes, var has a lot of idiosyncrasies. That's why we use const now, but the book spent two sentences offhandedly mentioning const.

JavaScript has a lot of baggage from its early days, but I would argue that learning the intricacies of how people used to write JavaScript isn't very useful today. Install ESLint, enable some recommended packages, and forget that it's possible to use those old features.
Profile Image for Anh Trinh.
53 reviews1 follower
March 29, 2020
This is a great book, give me a lot of new knowledge. The concept of the scope of JavaScript is quite different from another programming language. Sometimes this book is quite wordy but I think that's because of the purpose to explain for very new programmers. And I think more comments on code example as Head First Series will make this book perfect. For developers who learned or make familiar with another programming language before, I think you should remove it from your mind temporarily to understand the scope of JS.
Profile Image for Nishant Thakur.
41 reviews
July 6, 2023
This is a gorgeous book to understand scopes, closures, and how they are used for module patterns in JS. This is an extremely dense book and will require a few re-reads to understand it completely.

I noticed that contents of v2 of this book (You Don't Know JS Yet: Scope & Closures) looks different and will give that a read too in the future.
25 reviews
June 10, 2020
Deep and concise explanations of scopes and closures in Javascript, as well as a (useful and necessary) primer on how programming languages work in general. The book doesn't just teach you about good and bad coding practices, but helps you to develop a natural understanding of how things should be done by explaining the underpinnings of the Javascript language.

If you never quite understood hoisting or the difference between const, let and var - read this! I've never encountered a clearer explanation than this book.
Displaying 1 - 30 of 132 reviews

Can't find what you're looking for?

Get help and learn more about the design.