1. Repeated declarationvar supports repeated declaration, but let and const do not support repeated declaration. 1.1 varvar a = 1; var a = 2; console.log(a); Output:
1.2 letlet b = 3; let b = 4; console.log(b); Output:
1.3 constconst c = 5; const c = 6; console.log(c); Output:
2. Variable promotionvar supports variable promotion, but only promotes the declaration and not the value. let and const do not support variable promotion. 2.1 vara=2; console.log(a); var a = 1; Output:
2.2 leta=2; console.log(a); let a = 1; Output:
2.3 consta=2; console.log(a); const a = 1; Output:
3. Temporary dead zone There is no temporary dead zone for var, but there is a temporary dead zone for let and const. 3.1 varvar a = 1; function fun() { console.log(a); var a = 2; } fun(); Output:
3.2 letlet a = 1; function fun() { console.log(a); let a = 2; } fun(); Output:
3.3 consetlet a = 1; function fun() { console.log(a); const a = 2; } fun(); Output:
4. Properties and methods of the window objectIn the global scope, variables declared by var and functions declared by function will automatically become properties and methods of the window object. var a = 1; function add() { }; console.log(window.a === a); console.log(window.add === add); Output:
5. Block Scope var does not have block-level scope, but let and const do. for (var i = 0; i < 3; i++) { // console.log(i); } console.log(i); Output:
Use for (let i = 0; i < 3; i++) { // console.log(i); } console.log(i); Output:
This concludes this article about the differences between the usage of var, let, and const in JavaScript. For more information on the usage of JavaScript var, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation of automatic completion of Docker commands
>>: HTML table markup tutorial (14): table header
1. Introduction MySQL locks can be divided into g...
Today I was asked what the zoom attribute in CSS ...
Copy code The code is as follows: <thead> &...
Table of contents 1. ChildNodes attribute travers...
Table of contents Preface The difference between ...
XHTML is the basis of CSS layout. jb51.net has al...
Searching online for methods to deploy Angular pr...
11. Use JavaScript to create page effects 11.1 DO...
Stored Procedures 1. Create a stored procedure an...
1. Add skip-grant-tables to the my.ini file and r...
1. What problems did we encounter? In standard SQ...
In the Linux environment, you want to check wheth...
Table of contents Preface 1. Optimistic Locking A...
When writing the HTTP module of nginx, it is nece...
The method of using CSS style to vertically cente...