The difference between a function declaration and a function expression

Source: Internet
Author: User

When the parser loads data like an execution environment, it is not the same for function declarations and function expressions, and the parser first reads the function declaration and then executes the call to the function.

1: function declaration

alert (sum10,10);

function sum (num1,num2) {

return num1+num2;

}

This function works as expected, because the parser will raise the declaration function to the top of the source tree before the code executes.

Therefore, the function is executed first, and then the call to execute the function

2. Function expressions

Alert (sum 10, 10)

var sum=function sum (num1,num2) {

return num1+num2;

}

The above code generates an error during run time, because the reference to the function is not saved in the variable sum until the statement that contains the function is executed.

And because the first line of code can also cause errors.

There is no other difference than when you can pass the variable access function.

The difference between a 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.