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'; };