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
Overview The project was created successfully and...
I was working on a pop-up ad recently. Since the d...
To draw a table in HTML, use the table tag tr me...
Question How to access the local database in Dock...
Table of contents First of all, you need to know ...
Table of contents 1. Docker configuration 2. Crea...
Today's campus recruitment written test requi...
Deleting a single table: DELETE FROM tableName WH...
There are also two servers: Preparation: Set the ...
Table of contents Preface Option 1: Option 2: Opt...
Color contrast and harmony In contrasting conditi...
Without further ado, let's get straight to th...
need Add a paging bar, which can jump to the page...
bmi Voyager Pitchfork Ulster Grocer Chow True/Sla...
Ubuntu 20.04 has been officially released in Apri...