1) function declaration (functions Declaration);
function declaration functions fundeclaration (type) { return type=== "Declaration"; }
2) function expression.
function Expression var funexpression = function (type) { return type=== "expression"; }
There is a difference between function declarations and function expressions in Javascript, function declarations are promoted in JS parsing, so in the same scope, the function can be called regardless of where the function declaration is defined. The value of the function expression is determined at the JS runtime and is called after the expression assignment is complete. This tiny difference may lead to unexpected bugs in the JS code, which can cause you to fall into an inexplicable trap. The following code:
1 fundeclaration ("Declaration");//=> true2 function fundeclaration (type) {3 return type=== " Declaration "; 4 }
1 funexpression ("Expression");//=>error2 var funexpression = function (type) {3 return type=== " Expression "; 4 }
The above error occurs because a function created with a function declaration can be called after a function is parsed (parsing is handled logically), whereas a function created with a function expression is assigned at run time and cannot be called until the expression has been assigned to a value. The essential reasons are the differences between these two types in JavaScript function hoisting (function promotion) and runtime timing (parsing/runtime).
The function elevation of the above two pieces of code can be signalled as:
The code 1 Segment JS function is equivalent to:
function Fundeclaration (type) { return type=== "Declaration"; } Fundeclaration ("Declaration");//=> True
The code 2 Segment JS function is equivalent to:
var funexpression; Funexpression ("expression");//==>error funexpression = function (type) { return type=== "expression"; }
The difference between a function declaration and a function expression in JavaScript