JavaScript defines the difference between a function statement and an expression

Source: Internet
Author: User

Usually we see the following two ways to define a function:

// function Statements function fn (str) {  console.log (str);}; // definition of an expression var fnx=function(str) {  console.log (str+ ' from FNX ');};

Both methods create a new function object, but the function name of the function declaration statement is a variable name, and the variable points to the function object.
As with var declaration variables, functions in function definition statements are displayed to the top of the script or function, so they are visible throughout the script and function, but using the var expression to define the function,
Only the variable declaration is in advance, the variable initialization code is still in place, the function created with the function statement, the function name and the function body are advanced, so we can use it before declaring it.

Console.log (typeof(FN));//functionFN (' abc ');//ABCConsole.log (typeof(FNX));//undefined    if(FNX) FNX (' abc ');//Would not execute    ElseConsole.log (' Fnx is undefined ');//Fnx is undefined    //function Statements    functionfn (str) {console.log (str);    }; //definition of an expression    varfnx=function(str) {console.log (str+ ' from FNX '); };

Related Article

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.