Function Statements, Declarations and Expressions
Function Declaration/Statement
function whatDoYouDo (name, job) {
// code goes here
}
Function Expression
let whatDoYouDo = function (name, job) {
// code goes here
}
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.