Home > UI Developer > How do JavaScript closures work?

How do JavaScript closures work?


Whenever you see the function keyword within another function, the inner function has access to variables in the outer function.

 

function foo(x) {
  var tmp = 3;

  function bar(y) {
    console.log(x + y + (++tmp)); // will log 16
  }

  bar(10);
}

foo(2);
Categories: UI Developer
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment