JS variable Promotion

Source: Internet
Author: User

First, let's take a look at 2 pieces of code:

1.

function value () {return 1;} var Value;alert (value);

2.

function value () {    return 1;} var value = 1 ; alert (value);

You can think about what the results of 1 and 2 would be.

Let's look at one more topic:

var uname = ' Jack '    function Change () {        alert (uname);         var uname = ' Lily '        alert (uname)    } change    ();

The first uname will pop up defined, a lot of people don't understand why. In fact, this is the variable elevation of JavaScript;

Let me first declare 2 variables:

var a = ' a '; var b = ' B ';

In fact, that's how he interprets it:

So the above code will parse into this

var uname = ' Jack '    function change () {       var uname;        alert (uname);        uname = ' Lily ';        Alert (uname)    } change    ();

So we'll know why the first pop-up undefined.

Function declarations are also ahead of the variable declaration. We often see this way of writing:

a (); function A () {}

Or

function A () {}a ();

There is no problem with either of these two formulations. But if the function expression is not, then look down:

var temp  function() {    console.log (111);}

When you see this, you know the elevation of the variable declaration and the function declaration. function expressions are not promoted.

Well, let's answer it. What was the answer to the first 2 questions? Does everyone know the principle?

JS variable Promotion

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.