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 let
and const
commands 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> let
It 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
const
Declare a read-only constant. Once declared, the value of the constant cannot be changed.
const
The scope of the let
command is the same as that of the command: it is only valid within the block-level scope in which it is declared.