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

Source: Internet
Author: User

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

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.