Notes

Function Statements, Declarations and Expressions

Edit on GitHub

JavaScript

Function Declaration/Statement

1function whatDoYouDo (name, job) {
2  // code goes here
3}

Function Expression

1let whatDoYouDo = function (name, job) {
2  // code goes here
3}

A Function Expression defines a function as a part of a larger expression syntax (typically a variable assignment )

Differences

  • Function declaration/statements always have a function name whereas in function expressions the function is usually without a name i.e. anonymous. (named functions are easier to debug)
  • Function declaration are hoisted to the top of the enclosing function or global scope (meaning you can use the function before you declared it). Function expression are NOT hoised.