Skip to content

Instantly share code, notes, and snippets.

@belmer
Created April 1, 2021 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belmer/c69bc0124d44322acbb83b9857cdd7a0 to your computer and use it in GitHub Desktop.
Save belmer/c69bc0124d44322acbb83b9857cdd7a0 to your computer and use it in GitHub Desktop.
Useful javascript tips

IIFE

const increaseCounter = (function() {
  let counter = 0;
 
  return function() {
    counter = counter + 1;
    console.log(counter);
  };
})();

The IIFE run only once and initialize the counter variable with 0 then returns a function that can access the counter variable. So, when you call increaseCounter(), it will update the counter as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment