Getting started with ES6 basics (new let and const variables)

Table of contents

1. Introduction to ES6

1. ES6 concept

2. Why use ES6

Two, let variable

1.let is block-level scope

2.let does not have variable promotion

 3. "Temporary dead zone"

 4. Duplicate statements are not allowed

 5. Summary

Three, const variable

1. const has block scope

2. const declaration constants must be assigned

3. Constant assignment cannot be modified

4. The difference between let, const and var


1. Introduction to ES6

1. ES6 concept

The full name of ES is ECMAScript, which is a standardized specification of scripting language formulated by ECMA International Organization for Standardization.

ES6 is not only a historical term, but also a general reference. It means the next-generation standard of JavaScript after version 5.1, covering ES2015, ES2016, ES2017, etc., while ES2015 is the official name, specifically referring to the language of the official version released that year standard.

2. Why use ES6

The birth of each standard means the improvement of language and the enhancement of functions. There are also some unsatisfactory aspects of the JavaScript language itself.

- The variable hoisting feature increases the unpredictability of program runtime.

- The syntax is too loose, to achieve the same function, different people may write different codes.

Two, let variable

1.let is block-level scope

			 let str = "abc";
			 if(true){
			 	let str="123";
				console.log("if:",str);
			 }
			 console.log("全局:",str);

If the value defined by let is used in the if, the output is still the defined value, and the output defined globally is still the globally defined value. Although the same variable is defined, since let is defined as a block element, the output values ​​​​do not interfere with each other.

2.let does not have variable promotion


             console.log(i);
			 let i=1;

Let does not promote variables in the scope like var, and the variables declared by let will not be promoted, so a syntax error is thrown.

 3. "Temporary dead zone"

            var i=1;
			 if(true){
				 i=2;
				 let i=3;
			 }

 As long as there is a let command in the block-level scope, the variables it declares are "bound" to this area and are no longer affected by the outside world.

 4. Duplicate statements are not allowed

 let a=1;
 let a=3;

Repeatedly declaring a variable in a block scope will directly report an error.

 5. Summary

-- The let keyword is used to declare block-level variables.

-- The feature is that the {} declaration has block-level scope, and the var variable does not have this feature.

-- Prevent loop variables from programming global variables.

-- The let keyword has no variable hoisting.

-- The let keyword has the characteristics of a temporary dead zone. (declared before use)

Three, const variable

const has most of the same properties as let.

1. const has block scope

var a=1;
if(true){
	const a=2;
	console.log(a);
}

    The output must be 2 

2. const declaration constants must be assigned

if(true){
	const a;
	console.log(a);
}

 A direct error is reported for the lack of an initializer in the const declaration.

3. Constant assignment cannot be modified

const person = {};
person.name ="张三";
person.age=18;	  
person = {};

The object itself is mutable, so attributes can be added, but the address cannot be changed, and constant variables are assigned.

4. The difference between let, const and var 

- Variables declared with var are scoped within the function where the statement is located, and there is a phenomenon of variable promotion

- Variables declared using let have the scope of the code block where the statement is located, and there is no variable promotion

- Use const to declare a constant, and the value of the constant cannot be modified in the code that appears later

- Variables declared using let and const do not attribute to the top-level object and return undefined.

Guess you like

Origin blog.csdn.net/qq_65715980/article/details/125741568