let's features:
1, there is a local scope in {};
2, no variable lift;
const features:
1, the variable does not exist to enhance the problem, you can use this variable only after the definition;
2, the constants const definition, can not be reassigned;
3, when the time constant is defined, you must define initialization, otherwise it will error;
4, there are defined a constant const block-level scope.
Code Display
for (let i = 0; i < 5; i++) {
const a = "hello";
console.log(a);
}
console.log(a);
Output
let's application
When we click on this button three times output is: "I was the first three buttons"
<body> <input type="button" value="按钮1"> <input type="button" value="按钮2"> <input type="button" value="按钮3"> <script type="text/javascript"> var btns = document.getElementsByTagName("input"); for (var i = 0; i < 3; i++) { btns [I] .onclick = function () { the console.log ( " I was the first " + (I ) + " buttons " ); }; } </ Script > </ body >
Es6 solution: Use let you declare a variable i
<body> <input type="button" value="按钮1"> <input type="button" value="按钮2"> <input type="button" value="按钮3"> <script type="text/javascript"> var btns = document.getElementsByTagName("input"); for (let i = 0; i < 3; i++) { btns [I] .onclick = function () { the console.log ( " I was the first " + (I) + " buttons " ); }; } </ Script > </ body >
Output