var message;
var user;
console.log(message);// undefined
函数退出后,变量销毁.函数
function test(){
var msg='hi'; // 局部变量
}
test();
console.log(msg);// 错误
函数体内未定义只赋值的变量是全局变量:spa
function test(){
msg='hi';// 全局变量 不太推荐 由于在局部定义全局变量 难以维护
}
test();
console.log(msg);// 'hi'
变量提高(只是声明提高,赋值(初始化)没有提高):code
function test(){
console.log(msg);
var msg='hi';
};
test();// undefined
console.log(msg);// msg is not defined
由此能够看出变量 msg 提高到了函数test()的顶部,初始化并无提高,以下:
function test(){
var msg; console.log(msg); msg='hi'; };