And let the difference between the var JS

And let the difference between the var

1. no block by the scope of the declared variable var keyword, the variables declared in the block {} can be accessed from outside the block.

    Block scope variables have let keyword variable declared by the statement in the {} block within the block inaccessible from outside

eg:

 

{ 
   Var x = 10 ; 
} 
// here to use x 

{ 
  the let x = 10 ; 
} 
// here x can not be used

 

 

 

2. By re-var keyword to declare variables cause problems, declare a variable in the block is also variable external variables outside the re-statement blocks covering blocks

    By re-declared variables let keyword does not re-declare variables outside the block, will re-declare a variable block scope

eg:

 

var x = 10 ;
 // here x is 10 
{ 
   var x = 6 ;
   // here x is 6 
}
 // here x is 6

 

var x = 10 ;
 // here x 10 
{ 
  the let x = 6 ;
   // here x is 6 
}
 // here x 10

 

 

3. By using variables other than var keyword will re-circulating a statement in the loop, covering variables outside the loop, it will save the changes to variables inside the loop

    By using the let keyword does not re-declare variables outside circulating in the circulation, re-declare a variable block scope, and will not save the changes to variables inside the loop

eg:

 

var I =. 7 ;
 for ( var I = 0; I <10; I ++ ) {
   // Some statements 
}
 // here, i is 10

 

i = 7 the let ;
 for (i = the let 0; i <10; i ++ ) {
   // Some statements 
}
 // where i 7

 

 

4. By belonging to the window object keyword defined global variables var

    By the let keyword defined global variables do not belong to the window object

eg:

 

var carname = "Porsche" ;
 // code here can be used window.carName

 

= carname the let "Porsche" ;
 // code here unusable window.carName

 

 

5.  By the variable var notice will lift to the top of

   By variable defined not let be raised to the top, in a statement on the use of variables before let it causes ReferenceError, variable from the beginning of the block has been in a "temporary dead zone" until the declaration. let Requirements must wait letafter the declaration statement executed, you can use variables

   In the code blocks letbefore the command to declare variables, the variables are not available. This is grammatically called "temporary dead zone" (temporal dead zone, referred to TDZ).

   The dead zone is temporary in nature, as long as one into the current scope, variables to be used already exist, but are not available, only till the emergence of lines of code to declare variables before they can obtain and use the variable.

eg:

// where var is 
the console.log (foo); // output undefined 
var foo = 2 ; 

// case let in 
the console.log (bar); // given a ReferenceError 
let bar = 2;

Part of the dead zone is hard to find

eg:

function bar (x = y, y = 2 ) {
   return [X, Y]; 
} 

bar (); // error, because when x = y, y has not been declared dead zone belonging to

Anti think, following this situation is not being given.

function bar(x = 2, y = x) {
  return [x, y];
}
bar(); // [2, 2]

 

and let var in common

    When the function declaration variables using var and let very similar, they have function scope

    If the declaration statement outside the block, then var and let very similar, they all have a global scope

 

Note:

   Allowed anywhere in the program of var re-declare JavaScript variables

   In the same scope, or in the same block by let again declare a variable var is not allowed

   In the same scope, or in the same block by let again declare a let variable is not allowed

   In the same scope, or in the same block by var re declare a let variable is not allowed

   Or blocks in different scopes by let variables are permissible redeclaring

  You can not declare the parameters in the function

eg:

function FUNC (Arg) { 
  the let Arg; 
} 
FUNC () // error 

function FUNC (Arg) { 
  { 
    the let Arg; 
  } 
} 
FUNC () // not given

 

Guess you like

Origin www.cnblogs.com/wjrelax/p/11238278.html