The difference between function declaration and function expression in JS

Source: Internet
Author: User

The function declaration in JS refers to the following form:

function functionname () {}

Such a way to declare a function, whereas a function expression is like an expression that declares a function, such as:

var functionname = function () {}

Perhaps a lot of friends in the reading of these two kinds of writing will produce doubts, these two are similar, in the application seems to be feasible, then they are what difference?

In fact, the parser of JS does not treat function declaration and function expression equally. For a function declaration, the JS parser will read first, ensuring that the declaration has been parsed before all code executes, and that the function expression, like a variable of other primitive types, will be parsed only when executed to a certain sentence, so in practice, they will be different. When you define a function using the form of a function declaration, you can call the method before the function declaration, and the latter, if you do so, will error.


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.