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
Install mysql under win10 1. Download MySQL from ...
Table of contents Overview Require URL of the app...
Table of contents 1. Three modes of binlog 1.Stat...
Problem: The overflow of the auto-increment ID in...
Table of contents Problems encountered during dev...
Recently, I encountered a problem of whether the d...
Example source code: https://codepen.io/shadeed/p...
CSS Operations CSS $("").css(name|pro|[...
Step 1: Add a secondary domain name to the Alibab...
Result: html <canvas id="starfield"&...
Preface In the process of using MySQL database, i...
I heard that there is an interview question: How ...
This article example shares the specific code of ...
1. position : fixed Locked position (relative to ...
Why are the scroll bars of the browsers and word ...