Let and const commands

Source: Internet
Author: User
Tags constant table name

the difference between Var/let/const:

var:
-Variable elevation exists.
-the declared global variable can modify the value anywhere.
-
Let (somewhat similar to a function scope):
-There is no variable promotion.
-Dominate block-level scopes.
-can only be used in block-level scopes.
-You cannot declare the same variable with let in the same scope (function scope and block level scope)

Const:
-There is no variable promotion.
-Defines a constant.
-Scope and let are the same.
-the declared variable pointer points to an address in memory that cannot be modified, but can modify the basic usage of its data structure let:

1. Can only be used in block-level scopes

Block-level scope
 {let
    a=10;
    var b=20;
   Console.log (a);
}
Console.log (A, b);

After Babel translation:
{
  var _a = ten;
  var b =;
}

Console.log (A, b); REFERENCEERROR:A is not defined

2. Use let in the For loop
Contrast:

for (let i=0;i<=10;i++) {

 }
 console.log (i);  I is not defined for

(var j=0;j<=10;j++) {

}
Console.log (j);//11 the

former is Babel translated: for
(var _ i = 0; _i <= 10; _i++) {}
Console.log (i);

Babel just as much as possible to convert the ES6 syntax to es5,_ should be a naming habit plus _ represents a private variable. So for table name let is only a block-level scope, the block-level total with the domain variable name and global made a distinction

var a = [];
for (var i = 0; i < i++) {
  A[i] = function () {
    console.log (i);//i is a global variable so output
  :
}
A[6] (); 10
var arr=[];
for (let i=0;i<=10;i++) {
    arr[i]=function () {
        console.log (i)
    }
}

arr[6] ();//6

Coming out of the back loop body and entering judgment.
In a For loop, the execution condition is a parent scope, and the loop body is a child scope
Break point for viewing, although let's define a variable that exists at the block-level scope, each time the loop creates a new variable but let i=0 only once. Because the I of each loop is a separate variable (the pointer points to a unique address in memory), the value of the closure record is unique so that the final result can be obtained.

3. There is no variable promotion

var case
Console.log (foo);//output undefined
var foo = 2;

Let's case
Console.log (bar);//error referenceerror let
bar = 2;

At the compile time of the function, all variables declared by VAR are identified. In a global lexical environment, but without assignment, there is a variable elevation.
Let defines a variable that is initialized only when the script executes to a defined variable, so there is no variable elevation.
There is also the understanding that as soon as one enters the current scope, the variable to be used already exists, but is not available, and the variable can only be obtained and used until the line of code that declares the variable appears.

4. Temporary Dead Zone

var tmp = 123;

if (true) {
  TMP = ' abc ';//Referenceerror let
  tmp;
}

In the case of a var declared local variable, a scope chain is generated at the time of creation, the first object on the scope chain is the Global object window, the second object is his own scope (parameters, local variables, etc.), and the variable is read along his scope chain in order to look up.

But let is different, variables defined with let will "dominate" block-level scopes without external influence

Some of the effects of temporary dead zones:
1.typeOf is no longer a hundred-percent safe operation
2. Only claims can be obtained

5. Cannot be declared repeatedly in block-level scope

Block-level scope:

1. Disadvantages of not using block-level scopes:
The inner variable may overwrite the outer variable.
Inadvertently create global variables that pollute the global environment

Function F1 () {let
  n = 5;
  if (true) {let
    n = ten;
    Console.log (n)//10
  }
  console.log (n);//5 {{{{}

= insane = ' Hello world '}console.log (insane)}}}};//error
}
Block-level scopes cannot change variables that affect external, nor can external variables inside block-level scopes

2. Block-level scopes and function declarations

ES6 introduces a block-level scope that explicitly allows functions to be declared within a block-level scope. ES6 states that in block-level scopes, function declaration statements behave like let and are not referenced outside the scope of a block.
ES6 regulations

1. You can declare a function in a block-level scope (es5 not possible)
2. It is promoted to the head of the global scope or function scope.
3. The function declaration is also promoted to the head of the block-level scope at which it resides.

You can find that f is obtained before the function declaration, and the output is undifined.

function f () {Console.log (' I am outside! ');}

(function () {
  if (false) {
    function f () {Console.log (' I am inside! ');}
  }

  f ();
} ());

Equivalent to:

function f () {Console.log (' I am outside! ');}
(function () {
  var f = undefined;
  if (false) {
    function f () {Console.log (' I am inside! ');}
  }

  f ();
} ());

Summary: Try to avoid declaring functions in block-level scopes, and use function expressions if necessary. Const command:

The const command is used to define a constant.
1. Once the definition cannot be modified
2. Unable to increase the variable
3. Scopes and let are the same

Freeze objects

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.