Three variable declaration methods for VAR let const in JavaScript
1. var
①var indicates that a variable is declared and can be initialized at the same time .
② the scope of a variable declared with the Var statement is the context of the current execution position: the inside of a function (declared within a function) or globally (declared outside the function).
③ Assigning a value to a non-declared variable implicitly creates a global variable (causing code contamination)(a property of the global object). The difference between declaring a variable (declared Variable) and a non-declared variable (undeclared Variable) is:
(1) Declaring a variable is scoped to the context in which it is declared, rather than declaring the variable to be always global.
(2) Declaring a variable is created before any code executes, rather than declaring a variable that is created only when the assignment is performed.
(3) A declared variable is a non-configurable attribute (Non-configurable property) of its context, and a non-declared variable is configurable (for example, a non-declaring variable can be deleted).
It is strongly recommended that you use declarative variables!!! (whether or not within a function)
④ variable elevation: Declaring a variable anywhere in the code is always equivalent to declaring it at the beginning of the code because the variable declaration (and other declarations) are always processed before arbitrary code executes . This means that the variable can be used before the declaration.
2. Let
①let declares a local variable of a block-level domain and can give it an initialization value.
② let
allows you to limit the scope of a variable to a block-level domain. Unlike Var,var declares that variables are either global or function-level and cannot be block-level.
The variable scope declared by the ③let contains the block that defines it and any contained child blocks . This is similar to Var.
Why would ④ be the Let name? Https://stackoverflow.com/questions/37916940/js-why-let-have-this-name
⑤ on the top level of a program or function, let's behave similarly to Var.
⑥ defining A variable with let in the same function or in the same scope will cause typeerror.
The scope of ⑦let is a block , and the scope of VAR is a function .
⑧ provides a way to get the value of a variable within the bounds of a let块
block without affecting the value of the variable with the same name outside the block.
3. Const
The ①const declaration creates a read-only constant . This does not mean that the value that the constant points to is immutable , but that the value of the variable identifier can only be assigned once. ( note The difference between the value that distinguishes the constant and the value that the constant points to)
Note: This is similar to Java C + +. That is, a pointer to a constant indicating that the pointer does not change the value of the pointer to the variable, only "read-only". But the value of the variable being pointed to can be changed by other means.
② can declare constants within a global scope or function, and constants need to be initialized .
③ constants have block scopes and are very similar to variables defined with let. The value of a constant cannot be changed by re-assignment, nor can it be declared again.
④ A constant cannot have the same name as another variable or function in the same scope as it is located in.
The ⑤ constant can be a specific value , or it can be an object . Note: When a constant is an object, the properties inside the object are variable and not protected.
For example:
Const MYOBJECT = {"Key": "Value"};
MyObject = {"NewKey": "NewValue"}; This sentence is an error
Myobject.key = "NewValue"; This sentence will be successful.
Three variable declaration methods for VAR let const in JavaScript