Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

closure example

const add = (function () {
  let counter = 0;
  return function () {counter += 1; return counter}
})();

add();
add();
add();

// the counter is now 3
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #closure
ADD COMMENT
Topic
Name
5+9 =