The difference between a JS function declaration and a function expression

Source: Internet
Author: User
Tags js function declaration

The obvious difference:

1. A function declaration must begin with a function, and a functional expression must not be preceded by functions;

2. function declaration must have an identifier (function name), functional expression is optional;

The other difference:

1. The function declaration promotes the identifier (declaration elevation), and the function expression is not promoted;

2. function declarations and function names in named function expressions can be called inside the function itself, except that the named function expression cannot be called outside the function itself (not in the parent scope), but the function declaration can. As follows:

1  varex=functionexample () {2                  varA=1;3a++;4 Console.log (a);5 example (); Internal call valid, correct;6              }7 8 example (); Error, where the calling function is invalid; example is not declared here;9EX (); Here the call to ex is valid;Ten  One  A functionexample () { -                  varA=1; -a++; the Console.log (a); - example (); //Internal call valid, correct;  -              } -Example (); The call function here is valid

The difference between a JS function declaration and a function expression

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.