Organizational Research By

Surprising Reserch Topic

How do get 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) {
    alert(x + y + (++tmp)); // will alert 16
  }

  bar(10);
}

foo(2);

asked Jun 30, 2015 in JAVASCRIPT by rajesh
0 votes
14 views



Related Hot Questions

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

...