Let and const in the ES6

Source: Internet
Author: User
We used to define variables using Var, and ES6 gives us two ways to let and const (const is defined as constants). Today I will summarize let and Const. Simple, big god bird me. One, let define variable.

We use a let-defined variable and a variable defined by VAR in es5 to differentiate.

1. Let-defined variable with block-level scope
What is a block-level scope, like {} curly braces, is a block-level scope. So let is declared within the block-level scope, which is not accessible outside of the block-level scope.

function A () {let
    B = ten;
    Console.log (b)  //10  can ask
a ();
Console.log (b);  Error  uncaught referenceerror:b is not defined

And if we change the above let to VAR declaration variable, there will be no error. This let-declaration variable is a good solution to our previous closure problem. I don't understand the problem of closures. Look at my previous article http://blog.csdn.net/webxiaoma/article/details/53213113
If we declare a variable with VAR, we will have the following

function A () {
    var b = ten;
    Function C () {
        b++;
        Console.log (b)
    } return

    C;
 }
 D = A ();
 D ();
 d ();//12

This is the case where the closure is formed when the variable d refers to function C. Variable B is not recycled immediately after being referenced, but it is stored. So every time you execute D (), the B variable will add up to one, and sometimes we need to change that, which we used to do.

function A () {
    var b = ten;
   (function C () {
        b++;
        Console.log (b)
    }) ()
}

A ();//
A ();//11

In fact, now we can use ES6 's let to write this

function A () {let
    B = ten;  We just need to change the first code's VAR to let.
    function C () {
        b++;
        Console.log (b)
    } return

    C;
 }
 D = A ();
 D ();
 d ();//11

2. Let-defined variables are not promoted.

The variable of let declaration is not promoted. For example, in ES5 we do this.

Console.log (a)//result Underfind
var a = 5;

The browser does not complain but underfind, this is because variable a is promoted, do not know the principle of variable ascension can look at my previous article: http://blog.csdn.net/webxiaoma/article/details/52431203

And we use let-declared variables like this to write a browser directly to the error

   Console.log (a)//uncaught referenceerror:a is isn't defined let
   a = 5;

3. Let-defined variables cannot be overwritten, but values can be modified.

Variables for let declarations cannot be modified, for example:

   Let A = 5;
   Let a =6;
   Console.log (a)//error ' A ' has already been declared

And if the variable is declared with Var, it will directly overwrite the value of a before. Although the variable of let declaration can not be modified, we can change its value
For example:

Let A = 5;
a =6;
Console.log (a)//result 6;
const defines a constant.

1. What are constants, constants cannot be modified in ES6.

Const-declared constants also have let
The above three features. The only difference is that a let-declared variable, a variable pointer, cannot be modified, but the value of the address that the pointer refers to can be modified, and the const-declared constant cannot be modified or the browser will be wrong.

Const A = 5;
 a =6;
 Console.log (a)//error assignment to constant variable.

The top code runs an error, and the variables declared by let and Var are not an error.

Es6 these writing, for me personally feel very good, convenient, to us to write code also to play a certain normative role.

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.