Reference - Learn Code Academy

First Class Functions - This essentially means that:

  1. Functions can be passed as an arguments to other functions.
  2. Functions can be assigned to a variable. i.e. Function Expression

2 Ways to create functions in JavaScript

Function Declaration

  • Just as Variable Declarations must start with “var”, Function Declarations must begin with “function”.
  • Functions created using this way can be accessed by their name. The function name is visible within its scope and the scope of its parent (which is good because otherwise it would be unreachable)
function foo() {
    console.log('Hello World!');
}

foo();

Function Expression

  • Function expression means assigning a function to a variable. It must NOT begin with the keyword "function".
  • Functions created using this way can be named or anonymous. However, they can only be invoked using the variable, NOT the function name.
  • If the function is named, then the function name is visible within its scope.
// Anonymous Function

var bar = function() {
    console.log('Hello World!');
}

bar();

// Named Function

var bar = function foo() {
    console.log('Hello World!');
    // foo() can be accessed here
}

bar();

results matching ""

    No results matching ""