The let and const keywords in es6 and their differences from the var keyword

1.let keyword

First put the let keyword to declare the characteristics of variables:

  1. The let keyword can bind variables to the current scope (usually inside { ... })

  2. Variables declared with the let command cannot be accessed outside the block-level scope

  3. It is not allowed to declare the same variable multiple times using the let keyword

  4. There is no variable promotion. Variables declared with the let keyword must be used after the declaration, otherwise an error will be reported

1. The let keyword can bind variables to the current scope (2. The declared variables cannot be accessed outside the block-level scope)

Needless to say, the function scope of js, variables declared with var or let within the function cannot be directly accessed by the outside world.

But the block-level scope of JS, such as: for(){ .. }, if(){ .. }, { .. }代码块etc. all belong to the block-level scope. Variables declared with the var keyword in these block-level scopes can be directly accessed outside the block-level scope Yes, this increases the risk of polluting global variables.

like this:

	var a = 1;

	if (true) {
    
    
		var a = 2
	}

	console.log(a) // a

Since the value of a is reassigned with the var keyword in the if loop, a is printed out as 2. We definitely don't want the global environment a = 1to be polluted, so we can use the let keyword to solve this problem:

	var a = 1;

	if (true) {
    
    
		let a = 2
	}

	console.log(a) // 1

At this time, a declared with let in the for loop is bound to the for loop block-level scope, and cannot be accessed outside the for loop code block .

3. It is not allowed to use the let keyword to declare the same variable multiple times

This is very easy to understand, that is, a variable cannot be declared multiple times with the let keyword.

The same variable a can be declared multiple times with different values ​​using the var keyword, but a variable declared using the let keyword can only be declared once, otherwise an error will be reported:

let a = 1
let a = 2 // Uncaught SyntaxError: Identifier 'a' has already been declared
4. There is no variable hoisting

Variables declared with the var keyword exist 变量提升问题as follows:

	console.log(a); // undefined
	var a = 1

The above var a = 1would be broken down into this:

var a 
console.log(a) // undefined
a = 1

This is variable promotion. The variable declared with the var keyword will be promoted to the top of the scope first, and a will be assigned a value of 1 when it is actually assigned.

The let keyword can avoid this problem, it will throw an exception early:

	console.log(a); // Uncaught ReferenceError: Cannot access 'a' before initialization
	let a = 1

2. The const keyword

The const keyword is used to declare a constant that is read-only. The value of a variable declared with const cannot be modified. However, it should be noted that the type of the declared variable is a basic type or a reference type.

  • The value of the basic type (Number, Boolean, String, etc.) is stored at the memory address pointed to by the variable, so it is equivalent to a constant.

  • Reference type (Object, Array, etc.) variables only store a pointer to the secretary data, const can only guarantee that the value pointed to this pointer remains unchanged, and it is uncontrollable whether its data structure changes

const关键字和let一样也可以把当前变量绑定到当前作用域中。

3. ES6 declares variables6中方法

ES5 only has two ways of declaring variables: varand function. In addition to adding letand constcommands, ES6 also adds two ways to declare variables: importand classcommands

Guess you like

Origin blog.csdn.net/vet_then_what/article/details/125515189