The difference between function declaration and function expression in JavaScript

Source: Internet
Author: User

  There are two ways to declare a function in JavaScript: function declaration and function expression . What's the difference between them?

  The difference is as follows:

(1), a function that is defined as a function declaration, the function name is required, and the function name of the function expression is optional.

(2), a function defined as a function declaration, a function can be called before a function declaration, and a function expression can only be called after a declaration.

(3), functions defined as function declarations are not true declarations, they can only appear in the global or nested in other functions.

Let's take a look at the specific examples below. What is the difference between the following two ways?

function Boo () {};

var bar = function () {};

Some people may know, some people do not know, for me, I am ignorant, so I think it is necessary to take notes.

In ECMAScript, there are two most commonly used methods of creating function objects, that is, using function expressions or using function declarations. In this regard, the ECMAScript specification makes it clear that the function declaration must always have an identifier (Identifier), which is what we call the function name, and the function expression can be omitted . Here, the answer is self-evident (the former is a function declaration, the latter is a function expression).

Maybe someone would ask if there is a function name in the second way, for example, var bar = function Boo () {}; It is also a function expression because it is part of the assignment expression.

  function declaration:
function function name (parameter: optional) {body}
function expression:
function name (optional) (parameter: optional) {function Body}

So, it can be seen that if the function name is not declared, it must be an expression , but if the function name is declared, how to determine whether it is a function declaration or function expression? ECMAScript is distinguished by context, and if function Boo () {} is part of an assignment expression, it is a function expression, if function Boo () {} is contained within a function body, or at the top of the program, Then it is a function declaration.

There is also a function expression, which is enclosed in parentheses (function Boo () {}), which is the reason for the expression because the parentheses () are a grouping operator whose interior can only contain expressions.

Let's look at an interesting example:

  

How did it end? When I first saw this, I did it wrong. Well, the result of the operation is this.

  

Why is this, because at the same time that the first call to FN (), the var fn variable does not exist as a property of the global object, and the anonymous function context that FN refers to is not initialized, so the call failed before him.

Now tweak the code a little bit:

  

The result will be different.

  

The difference between function declaration and 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.