The difference between a function declaration and a function expression in JavaScript

Source: Internet
Author: User

definition of function expression in 1.js

expression, a phrase in JavaScript that JavaScript computes (evaluate) a result. A constant in a program is the simplest expression. The variable name is also a simple expression whose value is the value assigned to the variable. Complex expressions are made up of simple expressions.
--excerpt from the JavaScript Authority Guide

A classic method of defining a function expression:

//函数表达式的声明,表达式的值就是这个新定义的函数varfunction(){};

The function name is the part of the function declaration statement that must be made. It is used like the name of the variable, and the newly defined function object is assigned to the variable. For a function definition expression, the name is optional: if present, the name only exists in the function body and refers to the function object itself.
Note:
Defining a function with an expression only applies to it as part of a large expression, such as defining a function in the assignment and invocation procedure.

declaration of the function expression, the value of the expression is the newly defined functionvarExpressfunc = function(){};//Function declaration function ABC(){}//function declaration for incoming parameters function ABC2(test){}//Function Expression entry/exit parametersvarExpresstest = function(test){};The //function expression can contain a name, which is the actual assignment of the result function to ExpresstestvarExpresstest = function evalu(test) {    if(test) {return 2; }Else{return-1; }};//console.log (Evalu ()); Wrong way to callConsole.log (Expresstest ());//function expression passed as a parameter to other functionsData.sort ( function(A, B){returnA-B;})

Functions are defined in the form of function expressions, and the names of functions are optional. If a function name is defined, the function name becomes a local variable inside the function (well suited for recursion). function definition expressions are particularly well-suited for defining functions that are used only once.

The difference between a function declaration and a function expression in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.