1. var
Scope: The variable declared by var has a function scope, and if it is declared outside of the function, it will have a global scope. Variables declared with var in the global scope are appended to the window object.
Variable Boost:Variables declared by var are hoisting, meaning that no matter where part of the function is declared, they are moved to the top of the function.
Duplicate Declaration:The same variable can be declared repeatedly using var.
Reassign:Variables declared with var can be reassigned.
2. let
Scope:The variable declared by let has a block-level scope, which is only valid within the ** block in which it is declared.
Variable Boost:Let declared variables are also promoted, but they are not initialized. They are inaccessible until they are executed to the declaration, and this interval is referred to as a "temporal dead zone" (tdz).
Duplicate Declaration:In the same scope, let does not allow re-declaration of variables that already exist.
Reassign:Variables declared with let can be re-assigned, but cannot be declared repeatedly.
3. const
Scope:As with let, the variables declared by const also have block-level scopes.
Variable Boost:const likewise rises to the top of the block, but they are also inaccessible until the statement is declared, existing in a "transient dead zone".
Duplicate statement: const does not allow multiple declarations of variables within the same scope.
Reassign:Variables declared by const cannot be reassigned, they must be initialized at the time of declaration, and the values are fixed after the declaration. However, if the PERSIST variable points to an object or array, then the contents of the object or array can be modified.
Attached to the window object
In the browser environment, variables declared with var in the global scope become properties of the window object. This means that if you declare var dog='bowser', in fact you add a new global variable dog to the window object, which you can use via windowdog accesses it, and will get'bowser'this value.
In contrast, variables declared by let and const are not added to the window object. This helps avoid contamination of the global namespace and allows for more restrictive control of variables.