Understanding Variable Scope & Closures Without Losing your Mind

By Daniel Zabinskas

In this Learnable video you’ll learn about variable scope in JavaScript. How, when, and where you declare a variable dictates how you can access that variable later in your code. We’ll also see how understanding variable scope leads on to the extremely powerful concept of closures.

  • Ricardo Matias

    Kudos for the tutorial! I just didn’t get one thing, why did you create the function generateClickHandler ? Couldn’t you just have done that with clickHandler(i) ?

    • Knight Yoshi

      Yes, it would be possible to pass an argument to clickHandler(arg) instead of wrapping it in another function, but I think, I could be wrong, that it was done this way to show function scope.

  • DJ

    I would love to watch but I can’t get the video to play at all. (and I paused any plugins that would block it).

  • OphelieLechat
    • DJ

      Yeah. I ripped the URL from the source too. No go. Must be one of my plugins blocking it but since I have no problem with videos from “normal?” hosting sites I really have no inclination to start wasting time figuring it out.

      Thanks for the reply though.

  • Mohd. Mahabubul ALam

    JavaScript is a powerful language, but it does have its… wrinkles.
    Variable scopes and closures certainly fit into that category. Often
    asking the same questions in different places can get you very different
    answers. Daniel walks you through some examples and explains why it all
    makes sense. Mostly.



Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.