es6 var, let, const commands

1. let and var

  <1> Variables declared by let are only valid within the block-level scope;

    Variables declared by var are globally valid;

 

  <2> var variables are happy to be used before declaration, output undefined;

    let can't, throw an error directly;

 

E.g:

  //var 声明

  console.log(a);   //undefined
  var a = 55;

  // let statement 

  console.log(b);    // Error ReferenceError 

  let b = 67;

 

Note: The part of the loop variable of the for loop is a parent scope, and the inside of the loop body is a separate child scope.

ES6 clearly stipulates that if there are letand constcommands in a block, the variables declared by this block for these commands form an enclosing scope from the beginning. Anytime these variables are used before they are declared, an error will be reported.

  <3> letIt is not allowed to declare the same variable repeatedly in the same scope.

  E.g:

    

function func(){ 

    // Error 
  var a;

  let a;

}

function func(arg) {
    let arg; // Error report 
}

 

 

2.const

  constDeclare a read-only constant. Once declared, the value of the constant cannot be changed.

   constThe scope of the letcommand is the same as that of the command: it is only valid within the block-level scope in which it is declared.

 

Guess you like

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