Variables declared by let and Const are valid only within the code block
{Let
a = ten;
var b = 1;
}
A//REFERENCEERROR:A is not defined.
b//1
There is no variable elevation
Variables must be used after declaration, otherwise the error
var tmp = 123;
if (true) {
TMP = ' abc ';//Referenceerror let
tmp;
}
Do not allow duplicate declarations
Error
function () {Let
a = ten;
var a = 1;
}
Block-level scopes
function f () {Console.log (' I am outside! ');}
(function () {
if (false) {
///Repeat once functions f
() {console.log (' I am inside! ')}
f ();
} ());
I am inside! ES5 function Elevation
//i am outside! ES6 Block-level scopes
Const command
Declares a read-only constant, and once declared, the value of the constant cannot be changed
Once a variable is declared, it must be initialized immediately and cannot be left to be assigned later
Let command, const command, global variable declared by Class command, not part of global object's properties
var a = 1;
If in the node's REPL environment, can write GLOBAL.A//
or adopt the common method, writes THIS.A
WINDOW.A//1 let
b = 1;
WINDOW.B//undefined
I'll introduce you to ES6 's const command.
ECMA has always been the core of JS has no constant concept, es6 to make up for this one flaw;
Const foo= ' foo ';
foo= ' bar ';//typeerror:assignment to constant variable.
The example above declares a constant of the base type, such as an error if you try to modify the initial value, or if the value of the reference type is the same, but one thing to note:
Const foo=[];
foo=[1];//assignment to constant variable.
Normal error, no problem, look again:
Const foo=[1,2,3];
foo[1]=4;
Console.log (foo)//[1, 4, 3]
How come there's no error? And it can be modified? These two examples are different in that the former is modified pointer (need to familiarize with JS reference type) corresponding content changed, the latter is not pointing to still unchanged but the content of the object changed, for Foo, I'm just a pointer responsible for the corresponding object As for what the object content is, it's none of my business, so you can modify it; If you don't want the content to change, it's OK to use another method;
Const Foo=object.freeze ([1,2,3]);
foo[1]=4;
Console.log (foo)//[1, 2, 3]
So there is no need to worry about being modified;