The respective characteristics and differences of var and let and const

The difference between var and let

1. The characteristics of var

  1. var can be assigned first and then defined (the loophole is not logical)
age = 18
// let age   //let不能先赋值在定义,必须先定义再赋值,不然会报错说没有初始化
var age
console.log(age);

2. Variable promotion: var can be used first and then declared without error reporting (illogical)

console.log(a); // undefined 
var a = 10; //
编译过程 var a;
console.log(a); // undefined 
a = 10;
  1. var can be declared repeatedly, and subsequent declarations will override previous ones
var a = 10; 
var a = 20;
console.log(a); // 20
//  let不能重复声明
// let a = 1
// let a = 2错误
  1. var has no block-level scope. When var is used to declare a variable in a function, the variable is local
var a = 10;
function change(){ 
    var a = 20;
} 
change(); 
console.log(a); // 10

And if you don't use var inside the function, the variable is global

var a = 10; 
function change(){
    a = 20
}; 
change(); 
console.log(a); // 20

2. The characteristics of let

  1. There is no variable promotion, and the variable cannot be used before let declares it (temporary dead zone)
console.log(a); // ReferenceError: a is not defined let
a = 10;

2.let cannot be assigned before definition, it must be defined first and then assigned

age = 18
// let age   //let不能先赋值在定义,必须先定义再赋值,不然会报错说没有初始化
var age
console.log(age);
  1. It is valid in the code block where the let command is located, and it is valid in the block-level scope
{ 
    let a = 10;
}
console.log(a); // ReferenceError: a is not defined
  1. let does not allow repeated declarations in the same scope, note that it is the same scope, and repeated declarations in different scopes will not report an error
let a = 10;
let a = 20; // Uncaught SyntaxError: Identifier 'a' has already been declared 
let a = 10;
{ 
    let a = 20;
} // ok

Three.const

  1. const must be initialized
const a; // SyntaxError: Missing initializer in const declaration 
const a = 10; // ok
  1. const declares a read-only variable, which cannot be reassigned after declaration
const a = 10;
a = 20; // TypeError: Assignment to constant variable.
  1. const does not mean that the value of the variable cannot be changed, but that the data stored in the memory address pointed to by the variable cannot be changed
const obj = { 
    age: 17 
}
obj.age = 18; // ok
obj = { 
    age: 18
} // SyntaxError: Identifier 'obj' has already been declared
  1. Const has the characteristics that let should have

Four. Difference

1. Assign first and then define

var can be assigned first and then defined, let cannot be assigned first and then defined, it must be defined first and then assigned, otherwise an error will be reported saying that there is no initialization

2. Variable promotion

The variable declared by var has variable promotion, that is, the variable can be called before the declaration, and the value is undefined
let and const. There is no variable promotion, that is, the variables they declare must be used after the declaration, otherwise an error will be reported

3. Block-level scope

var does not have block-level scope
let and const have block-level scope

4. Repeat statement

var allows repeated declaration of variables
let and const do not allow repeated declaration of variables in the same scope

5. Modify the declared variable

var and let can
const declare a read-only constant. Once declared, the value of the constant cannot be changed, but for reference types such as objects and data, the memory address cannot be modified, and the value inside can be modified.

Guess you like

Origin blog.csdn.net/qq_52006046/article/details/128744259