JS in Var and let

Source: Internet
Author: User

javascript

1.1
Non-assignment declaration
    <Scripttype= "Text/javascript">        //Test Code        varA;                Let B; Console.log ("this is the VAR statement:"+a); Console.log ("This is the Let statement:"+a); </Script>

Results:

As can be seen from the above code, when declaring a variable, the Let and Var are the same when they are declared, and are shown as: undefined.

1.2 repeatedly declaring a variable
    <Scripttype= "Text/javascript">        //Test Code        vara= 1; vara= 2; Console.log ("this is the VAR statement:"+a); </Script>

Operation Result:

    <Scripttype= "Text/javascript">        //Test CodeLet a= 1; Let a= 2; Console.log ("this is the VAR statement:"+a); </Script>
    <Scripttype= "Text/javascript">        //Test Code        vara= 1; Let a= 2; Console.log ("this is the VAR statement:"+a); </Script>

Operation Result:

     

As seen from the code above, when a variable is defined, both Var and let can again assign a value to the defined variable. However, a variable declared with VAR can be declared repeatedly n times, while a variable with let declaration can only be declared once, and, even if it is a var or other keyword-declared variable, it will also make an error if it is used.

1.3 function scope
    <Scripttype= "Text/javascript">        //Test Code        vara= 1; Let B= 2; C= 3; !function(){            vara= 4; Let B= 5; C= 6; Console.log ("This is the self-executing function var declaration a:"+a); Console.log ("This is the self-executing function let Declaration b:"+b);                }(); Console.log ("this is var declaration a:"+a); Console.log ("This is the Let declaration B:"+b); Console.log ("This is the global variable C:"+c); </Script>

Operation Result:

As you can see from the code and the results above, the performance of Var and let in the function scope is the same. A variable defined in a function can only be called in a function unless it is a global variable.

1.5 block scope

    <Scripttype= "Text/javascript">        //Test Code         for(varI=0; I<5; I++) {console.log (i);    } console.log (i); </Script>

Operation Result:

    

    <Scripttype= "Text/javascript">        //Test Code         for(Let I=0; I<5; I++) {console.log (i);    } console.log (i); </Script>

Operation Result:

    

    <Scripttype= "Text/javascript">        //Test CodeLet c= 3; Console.log ('Out-of- function let definition c:' +c); !function() {Let C= 6; Console.log ('in-function let definition C:' +c);        }(); Console.log ('after a function call, the LET definition C is not affected by the inner definition of the function:' +c); </Script>

Operation Result:

    

As seen from the code above, variables declared in the For loop can be placed out of the loop after the end of the loop, while a let declaration cannot be invoked outside the loop, prompting an error. It is mentioned that the variable of let declaration can not be repeated, but under Let's block scope, lets can declare the variable again, but the variable declared in the function can only be used in the function, that is, the two variables declared with let are two different variables. This is also the biggest difference between let and var.

JS in Var and let

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.