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
The effect is as follows:Reference Program: <!...
Table of contents Preface LED Trigger Start explo...
Preface When mysql modified the default database ...
nvm nvm is responsible for managing multiple vers...
Why did I use this? It all started with the makin...
This article shares the specific code of jQuery t...
Written in the opening: Absolute said: "Rela...
Preface Js is the most commonly used code manipul...
Nowadays, copying websites is very common on the I...
MySQL v5.7.19 official version (32/64 bit install...
As a basic element of a web page, images are one ...
This article records the installation and configu...
<br />When discussing with my friends, I men...
Preparation 1. Start the virtual machine 2. git t...
1. Download the MySQL installation package (there...