Javascript self-executing functions

Javascript self-executing functions

In Javascript, a function can be defined as so:

var myFunction = function(){
  console.log('hello world')
}

This function can be called using brackets as so:

myFunction()

which would print hello world in the console.

Sometimes, naming a function might not be necessary, for example if the function is just called once within the code. In such case, the function can be made self-executing using wrapping brackets:

(function(){
  console.log('hello world')
})()