javascript event-loop question javascript event-loop question node.js node.js

javascript event-loop question


JavaScript uses function scoping, the scoping rules are the same in all JS environments. As Nican mentioned understanding closure is important to knowing what is available in your current scope.

Basically a function "remembers" the environment in which it was defined. So if you use an inline anonymous function for your callback it will have access to all the variables that are available to its parent function and anything that is passed into it as an argument.

A few resources regarding closures and scope in JavaScript:

Stoyan Stefanov's book Object-Oriented JavaScript does a great job of explaining scoping in JavaScript and how the lexical scoping of functions work (see chapter 4). I'd recommend the book to anyone who is serious about JS programming.


There is a nice tool called Javascript Loupe created by Philip Roberts that will help you understand how javascript's call stack/event and loop/callback intereact with each other.Write some piece of javascript code in the editor and try to run it.