When it comes to the methods of declaring variables in Let's first talk about the differences between the three as a whole. Before introducing them in detail, the differences between var, let, and const are mainly analyzed from the following points:
As a global variable In However, variables declared with Variable Hoisting Variables declared with There is no variable promotion console.log(a) // undefinedvar a = 1console.log(b) // Cannot access 'b' before initializationlet b = 2console.log(c) // Cannot access 'c' before initializationconst c = 3console.log(a) // undefined var a = 1 console.log(b) // Cannot access 'b' before initialization let b = 2 console.log(c) // Cannot access 'c' before initialization const c = 3 Temporary dead zone There is no temporary dead zone There is a temporary dead zone In fact, this is the difference that is extended from the previous variable improvement. Because variables declared with Same as above: console.log(a) // undefined var a = 1 console.log(b) // Cannot access 'b' before initialization let b = 2 console.log(c) // Cannot access 'c' before initialization const c = 3 Block scope { var a = 2}console.log(a) // 2{ let b = 2}console.log(b) // Uncaught ReferenceError: b is not defined{ const c = 2}console.log(c) // Uncaught ReferenceError: c is not defined Duplicate Statement var a = 10 var a = 20 // 20 let b = 10 let b = 20 // Identifier 'b' has already been declared const c = 10 const c = 20 // Identifier 'c' has already been declared Modify declared variables (constants and variable declarations) var a = 10 a = 20 console.log(a) // 20 let b = 10 b = 20 console.log(b) // 20 const c = 10 c = 20 // Uncaught TypeError: Assignment to constant variable SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Website front-end performance optimization: JavaScript and CSS
>>: How to set MySQL foreign keys for beginners
How to refresh iframe 1. To refresh, you can use j...
Solution-1: Download the msvcr100.dll file (find ...
This article records the installation tutorial of...
How to determine whether the current Linux system...
Table of contents Bidirectional binding principle...
react-native installation process 1.npx react-nat...
Overview There are many open source network monit...
Dockerfile is a text file that contains instructi...
Lists are used to list a series of similar or rela...
This article shares the specific code for JavaScr...
Table of contents 1. What is a transaction? 2. Th...
Use wget command to download the entire subdirect...
#include <linux/moduleparam.h> 1. Module pa...
MyISAM, a commonly used storage engine in MySQL c...
Detailed explanation and examples of database acc...