Self-invoking Functions

Published on: January 7, 2017

Welcome to the 29th Easy JavaScript tutorial, part of EasyProgramming.net. I apologize for the static in this video! I've gotten a new mic since this was recorded.  

We'll briefly cover self-invoking functions in this tutorial. 

Self-invoking functions are exactly what they sound like, functions that invoke themselves.  Unless you loop through them, they usually just run once per script. This is a good workaround to defining the function somewhere and calling it from your script. self-invoking functions allow you to do both in one step. 

Syntax of a self-invoking function:

  (function(){
    //declare local variables
    //run this piece of code
    //invoke it by using ();
  })();

To fork the fiddle and follow along: https://jsfiddle.net/easyjs/Lre954nq/

 

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:

Comments: