function printName() {
if(true) {
var name = "Rafael";
}
console.log(name); // Rafael
}
是可以正常运行的
function printName() {
if(true) {
let name = "Rafael";
}
console.log(name); // ReferenceError: name is not defined
}
运行报错 name没定义
总结:var是function-scoped,而let是 block-scoped.
function printName() {
var name = "Hey";
if(true) {
let name = "Rafael";
console.log(name); // Rafael
}
console.log(name); // Hey
}