Skip to content

Instantly share code, notes, and snippets.

@khamiltonuk
Created February 16, 2014 09:48
Show Gist options
  • Select an option

  • Save khamiltonuk/9031896 to your computer and use it in GitHub Desktop.

Select an option

Save khamiltonuk/9031896 to your computer and use it in GitHub Desktop.
Creating functions
// Both ways to create functions use the function keyword, so the difference between them aren't immediately apparent.
//******************
// Declarations
//******************
// Variable declarations and functions declarations operate similiarly. They start with a keywords (var or function), followed by an identifier.
// Function declarations add lists of arguments in parentheses followed by blocks in curly braces.
function functionName(arg1, arg2, argN) {
// Block of statements
}
// Javascript interpreters look at functions declarations before evertything else in the script, so you can call them before you declare them.
myFunc();
function myFunc(){
// Statements
}
//******************
// Expresions
//******************
// You don't have to declare a name for a function and these are called 'function expressions'
// They are sometimes refered to as anonymous functions.
// You can store a function expression on a variable or use it as a return value in another function.
var functionName = function(arg1, arg2, argN) {
// Statements
};
function outerFunction() {
// This function returns an anonymous function
return function() {
console.log('inner function');
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment