1.变量作用域
例一
var a = 1;
function test() {
var a = 2;
console.log(a);
}
test();//2
就近原则
例二
function test2() {
console.log(a++);
var a = 2;
}
test2();
由于变量声明提升
例三
var a = 1;
function test2() {
console.log(a);
var a = 2;
}
test2();//undefined
例四
var a = 1;
function test3() {
console.log(a);
a = 2;
}
test3(); // 1
没有变量提升
例五
let b = 1;
function test4() {
console.log(b);
let b = 2;
}
test4();// b is not defined
let没有变量提升
例六
function test5() {
let a = 1;
{
let a = 2;
}
console.log(a);
}
test5();// 1