1. let keyword 1.1 Basic Usage let is a new keyword added in The syntax structure is as follows: let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]; Parameter Description: The sample code is as follows: In the global scope, variables are defined // define global variables var a = 100; // use the var keyword let b = 200; // use the let keyword console.log(a); // 100 console.log(b); // 200 We found that simply defining a global variable makes no difference. Defining variables in block scope For more information about block scope, please refer to this article. // Define variables in block scope { var a = 100; // uses the var keyword let b = 200; // uses the let keyword } console.log(a); // 100 console.log(b); // Throws an exception with the description ReferenceError: b is not defined If The The sample code is as follows: // Define a loop body for (let v = 0; v < 10; v++) { console.log("This is a for loop"); // This is a for loop * 10 } console.log(v); // Throws an exception with the description: ReferenceError: v is not defined It is worth noting that the The sample code is as follows: for (let v = 0; v < 10; v++) { let v = 10 console.log(v); // 100 * 10 } The above result means that the two v are not in the same scope. 1.2 There is no variable promotionIf you use var to define a variable, the so-called variable commission will occur, as shown in the following code: console.log(v); // undefined var v = 100; We use this value before the variable declaration, and it does not report an error, but the result is undefined. This is a strange logic. According to general logic, if it is used in this way, it should throw an exception. In As shown in the following code: console.log(v); // Throws an exception with the description ReferenceError: Cannot access 'v' before initialization let v = 100; In the above code, an exception will be thrown with the description that a variable cannot be used before being declared. 1.3 Temporary Dead Zone As long as there are variables defined with the let v = 100; { console.log(v); // Throws an exception with the description ReferenceError: Cannot access 'v' before initialization // This variable is bound to the block-level scope, and this scope can only use this variable let v = 200; }
In general, in a block-level scope, a variable is not available until it is declared using the let keyword. This is grammatically called a temporary 1.4 Duplicate declarations are not allowed The let keyword provided by // Use var to declare variables var a = 100; console.log(a); // 100 var a = 1000; // repeated declaration console.log(a); // 1000 // Use let to declare variables let b = 100; console.log(b); // 100 let b = 1000; // Repeated declaration console.log(b); // Throws an exception with the following description: SyntaxError: Identifier 'b' has already been declared If you declare it repeatedly, 1.5 Relationship with functions The function parameters we use are equivalent to variables defined using the let keyword. If you use the let keyword again in the function body to re-declare a variable with the same name as the parameter, // Use let to declare variables let a = 100; // define a function function fun(b) { // let b = 10; // throws an exception with the description: SyntaxError: Identifier 'b' has already been declared console.log(a); // Throws an exception with the description: ReferenceError: Cannot access 'a' before initialization let a = 200; } fun(20) 2. const keyword 2.1 Basic Usage The The so-called constant is immutable after initialization and must be initialized when declared. The sample code is as follows: const a = 2 a = 3 // throws an exception const b // uninitialized throws an exception However, when we use const arr = [] arr[0] = 100 console.log(arr) // [100] const obj = {} obj.name = 'A bowl of Zhou' console.log(obj) // {name: "Yiwan Zhou"} The reason for this problem is that the const keyword actually guarantees not that the value of the variable cannot be changed, but that the memory address pointed to by the variable cannot be changed. For simple data types, such as However, for composite data types, mainly In actual development, we avoid using the var keyword and instead use the This is the end of this article about the new features of JavaScript ES, let and const keywords. For more information about the new features of ES, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation steps for enabling docker remote service link on cloud centos
>>: The space is displayed differently in IE, Firefox, and Chrome browsers
This article summarizes the notes for installing ...
Table of contents Preface What are asynchronous i...
Table of contents 1. What is currying 2. Uses of ...
Table of contents In JavaScript , there are sever...
Table of contents 1. Source code 1.1 Monorepo 1.2...
This article shares the specific code for WeChat ...
First of all, this post is dedicated to Docker no...
How to indicate the parent directory ../ represent...
1: Baidu website login entrance Website: http://ww...
Everyone must be familiar with table. We often en...
Table of contents Install Importing components Ba...
Problem Description The MySQL startup error messa...
Table of contents 2 solutions for file upload Bas...
1. Preparation 1.1 Download and install VMware 15...
Blank's blog: http://www.planabc.net/ The use...