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. Problem Description root@mysqldb 22:12: [xucl]...
Table of contents Problem Description Solution Pr...
Preface This control will have a watermark at the...
Learn how to host your own website on Apache, a r...
1. First of all, we need to distinguish between t...
We are not discussing PHP, JSP or .NET environmen...
Table of contents 1. What is vuex 2. Installation...
1. Installation Search the mariadb version to be ...
Table of contents 1. What is a closure? 2. The ro...
MySQL is a powerful open source database. With th...
This article shares the specific code of JavaScri...
Date-type single-row functions in MySQL: CURDATE(...
Table of contents Demo1 create_fragment SvelteCom...
Table of contents Starting from type judgment Str...
<br />Structure and hierarchy reduce complex...