Difference between the function declaration and function expression in javascript, javascript expression
1. function expression definition in js
Expression is a phrase in JavaScript, and javascript calculates it (evaluate) to produce a result. Constants in a program are the simplest expressions. A variable name is also a simple expression. Its value is the value assigned to the variable. A complex expression is composed of simple expressions.
-- From javascript authoritative guide
How to define a classic function expression:
// Declaration of the function expression. The value of the expression is the newly defined function var expressFunc = function (){};
The function name is a required part of the function declaration statement. Its purpose is like the name of a variable. The newly defined function object will assign a value to this variable. For a function definition expression, this name is optional: If it exists, the modified name only exists in the function body and indicates the function object itself.
Note:
Defining a function using an expression only applies to it as a part of a large expression, such as defining a function during assignment and call.
// Declaration of the function expression. The value of the expression is the newly defined function var expressFunc = function () {}; // function declaration function abc () {}// function declaration of input parameters function abc2 (test) {}// function expression Entry and Exit parameter var expressTest = function (test) {}; // function expression can contain a name, here, the result function is actually assigned to expressTestvar expressTest = function evalu (test) {if (test) {return 2 ;}else {return-1 ;}}; // console. log (evalu (); // incorrect call method console. log (expressTest (); // The function expression is used as a parameter to pass to other functions. sort (function (a, B) {return a-B ;})
Define a function in the form of a function expression. The function name is optional. If the function name is defined, the function name becomes a local variable inside the function (suitable for recursion ). Function Definition expressions are especially suitable for defining functions that only need to be used once.