[ES6] The difference between var, let, and const

First, a common question is, what is the relationship between ECMAScript and JavaScript?

      ECMAScript is an internationally adopted standardized scripting language. JavaScript consists of ECMAScript, DOM, and BOM. It can be simply understood as: ECMAScript is the language specification of JavaScript, and JavaScript is the implementation and extension of ECMAScript.

        In 2011, ECMAScript version 5.1 was released. Most of us used ES5 before.

        In June 2015, ECMAScript 6 was officially adopted and became an international standard.


1. Block scope {}

        The scopes in ES 5 are: global scope, function scope. There is no concept of block scope.

        Block-level scoping was added in ES6 . Block scope is enclosed by { }, and { } in if statement and for statement also belongs to block scope.

<script type="text/javascript">
	
    {
        var a = 1;
        console.log(a); // 1
    }
    console.log(a); // 1
    // Variables defined by var can be accessed across block scopes.

    (function A() {
        var b = 2;
        console.log(b); // 2
    })();
    // console.log(b); // report an error,
    // Visible, variables defined by var cannot be accessed across function scopes

    if(true) {
        var c = 3;
    }
    console.log(c); // 3
    for(var i = 0; i < 4; i ++) {
        var d = 5;
    };
    console.log(i); // 4 (i is already 4 at the end of the loop, so i is 4 here)
    console.log(d); // 5
    // Variables defined with var in if statements and for statements can be accessed outside,
    // Visible, if statement and for statement belong to block scope, not function scope.

</script>


2. The difference between var, let and const

  1. Variables defined by var have no concept of blocks and can be accessed across blocks, but not across functions.
  2. Variables defined by let can only be accessed within the block scope, not across blocks, nor across functions.
  3. Const is used to define constants, which must be initialized when used (that is, must be assigned a value), can only be accessed in the block scope, and cannot be modified.
<script type="text/javascript">
    // block scope
    {
        var a = 1;
        let b = 2;
        const c = 3;
        // c = 4; // report an error
        var aa;
        let bb;
        // const cc; // error
        console.log(a); // 1
        console.log(b); // 2
        console.log(c); // 3
        console.log(aa); // undefined
        console.log(bb); // undefined
    }
    console.log(a); // 1
    // console.log(b); // error
    // console.log(c); // error

    // function scope
    (function A() {
        var d = 5;
        let e = 6;
        const f = 7;
        console.log(d); // 5
        console.log(e); // 6  
        console.log(f); // 7

    })();
    // console.log(d); // error
    // console.log(e); // error
    // console.log(f); // report an error
</script>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326303104&siteId=291194637