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
Table of contents 1. Introduction 2. Ideas Two wa...
Script requirements: Back up the MySQL database e...
1. Shut down the mysql service # service mysqld s...
Table of contents 1. Variables Use meaningful nam...
Often, we may need to export local database data ...
<br />In the past, creating a printer-friend...
In the actual project development process, the pag...
1. In addition to the default port 8080, we try t...
30 free high-quality English ribbon fonts for down...
1. Use CSS, jQuery, and Canvas to create animatio...
<br />This article will briefly introduce yo...
As we all know, mailto is a very practical HTML ta...
#docker search #docker pull portainer 1. Download...
Preface Recently I found that my friend's met...
Table of contents 1. Use of arrow functions 1. Fr...