Js variables: the difference between the three keywords var, let, and const

The difference between the three keywords var, let, and const

was

  1. Declaration scope: Inside the function, use var to define a variable (local variable), which will be destroyed immediately after the function is called. If var is omitted when defining a variable, a global variable will be created (it is not recommended to define global variables in the local scope, it is difficult to maintain, and in strict mode, a ReferenceError will be thrown).
  2. Declaration Hoisting: pulls all variable declarations to the top of the function scope.
function foo(){
    
    
    console.log(age);
    var age = 18;
}

Equivalent to

function foo(){
    
    
    var age;
    console.log(age);
    age = 18;
}
foo();//undefined
  1. You can use var to declare the same variable multiple times.
function foo(){
    
    
var age = 16;
var age = 1;
var age = 2;
console.log(age);
}
foo();//2
  1. Global declaration: Variables declared in the global scope using var become properties of the window object.
var age = 18;
console.log(window.age);//18

let

1. Declaration scope: let declares block scope, while var declares function scope. Block scope is a subset of function scope, so the restrictions on var scope also apply to let.

//var
if(true){
    
    
    var age = 18;
    console.log(age);//18
}
console.log(age);//18

//let
if(true){
    
    
    let age = 18;
    console.log(age);//18
}
console.log(age);//ReferenceError:age没有定义
  1. Declaration hoisting: variables declared with let are not hoisted in scope
  2. It is not allowed to use let to declare the same variable multiple times in a block
let age;
let age;//SyntaxError,标识符age已经声明过了
  1. Global declaration: Variables declared in the global scope using let will not become properties of the window object, but variables declared with var will.
let age = 18;
console.log(window.age);//undefined
  1. Conditional statement: When using var to declare a variable, since the statement will be promoted, js will automatically merge the redundant statement into one statement at the top of the scope. And because the scope of let is a block, it is impossible to check whether a variable with the same name has been declared using let before. So for let, you cannot rely on the conditional declaration pattern.
  2. The let declaration in the for loop: When using let to declare the iteration variable, js will declare a new iteration variable for each iteration variable in the background, and each setTimeout refers to a different iteration variable. Both for-in and for-of apply.
for(var i=0;1<5;i++){
    
    }
console.log(i);  //5

for(let i=0;1<5;i++){
    
    }
console.log(i);  //ReferenceError:i没有定义
for(var i=0;1<5;i++){
    
    
    setTimeout(()=>console.log(i),0);
}
//会输出5,5,5,5,5

for(let i=0;1<5;i++){
    
    
    setTimeout(()=>console.log(i),0);
}
//会输出0,1,2,3,4

const

  1. The behavior of const is basically the same as that of let. The only difference is that when const declares a variable, it must initialize the variable at the same time, and trying to modify the variable declared by const will cause a runtime error (TypeError).
  2. Duplicate statement not allowed
  3. scope is also block
  4. The restrictions of a const declaration apply only to references to the variable it points to. In other words, if the const variable refers to an object, then modifying the properties inside the object does not violate the const restriction.
const person = {
    
    };
person.name = "QiuYing";
  1. Iteration variables cannot be declared as const, because the iteration variables are self-incrementing. But it is possible to declare a for loop variable that will not be modified. This is especially meaningful for for-in and for-of loops.

Declaration style and best practice: use const first, let second, do not use var.

Guess you like

Origin blog.csdn.net/m1009113872/article/details/109563154