JavaScript variable declares the difference between Var and let

Source: Internet
Author: User
Tags button type

var declares a variable that is an entire region

Let declares that the variable is valid only in the local area.

123<script>4            functionMyFunction1 () {5                varA=1;6                if(1){7                    vara=2;8Console.log (a);//Output 29                }TenConsole.log (a);//Output 2 One            } A            functionMyfunction2 () { -Let A=1; -                if(1){ theLet a=2; -Console.log (a);//Output 2 -                } -Console.log (a);//Output 1 +            } -</script> + A<body> at&LT;H1 id= "H1" > - the difference between Var and let - -<p id= "P1" > - let allows you to declare a variable, statement, or expression that has a scope that is limited to the block level. The Let variable is recommended for local variables in function to avoid variable name collisions.  -</p> in<button type= "button1" onclick= "MyFunction1 ()" > Functions 1 Execution </button> -<button type= "Button2" onclick= "Myfunction2 ()" > Functions 2 Execution </button> to      +</body> -

From the output, we can see that the VAR declaration variable A can change his value in the If.

The variable a=1, declared in the IF, a=2 is two separate variables.

JavaScript variable declares the difference between 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.