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

Source: Internet
Author: User
Tags expression

The function declaration in JS refers to the following form:

function functionname () {  
      
}

This way to declare a function, and a function expression is like an expression to declare a function, such as:

var functionname = function () {  
}

Probably many friends in see these two kinds of writing will produce the doubt, these two kind of writing similar, in the application seemingly also all is feasible, then they have what difference?

In fact, the JS parser does not treat the function declaration and function expression equally. For function declarations, the JS parser will be read first, make sure that the declaration has been resolved before all the code executes, and that the function expression, like a variable that defines other basic types, is parsed only as it executes to a sentence, so in practice they are still different, specifically, When you use the form of a function declaration to define a function, you can invoke the method after the function declaration, and the latter, if you do so, will make an error.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

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.