Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 597 Bytes

anonymousFunctions.md

File metadata and controls

29 lines (21 loc) · 597 Bytes

Anonymous functions

Function that is declared without any named identifier. Therefore it is not accessible after its initial creation.

Example:

function(){
  console.log('boo')
}

Use case

Anonymous functions can be used for IIFE (Immediately Invoked Function Expressions) to encapsulate some code inside a local scope so that variables declared in it do not leak to the global scope.

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

They can also be used as callbacks:

setTimeout(function(){
  console.log('hello')
}, 1000);