Skip to content

Instantly share code, notes, and snippets.

@travishen
Last active August 26, 2018 10:08
Show Gist options
  • Select an option

  • Save travishen/22ea12502ddb6e8cf1b0bc3b2cfe4cba to your computer and use it in GitHub Desktop.

Select an option

Save travishen/22ea12502ddb6e8cf1b0bc3b2cfe4cba to your computer and use it in GitHub Desktop.
// Function Statement
greet(); // hoisting
function greet(){
console.log('hi');
};
// FUNCTION EXPRESSION (ANONYMOUS)
anonymousGreet(); // undefined is not a function
anonymousGreet; // undefined
var anonymousGreet = function(){
console.log('hi');
};
// wrap in parentheses
(function(){
console.log('hi');
});
// IMMEDIATELY INVOKED FUNCTION EXPRESSIONS (IIFE)
var IIFEGreet = function(name){
return 'hi ' + name;
}('John'); // hi John
IIFEGreet; // hi John
"hi John"; // hi John
IIFEGreet(); // IIFEGreet is not a function
// wrap in parentheses
(function(name){
return 'hi ' + name;
}('John')); // hi John
(function(name){
return 'hi ' + name;
})('John'); // hi John
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment