你问题的中的代码可以等价于下面的: a = 1; function test() { var a; console.log(a); // undefined a = 2; console.log(a); // 2 } `var a = 2; ` 语句包含了声明变量和给变量赋值,因为变量提升(hositing)会把变量声明提升到函数开始位置,而赋值语句不会提升。所以运行到第一个`console.log(a);` 时,因为函数已经有 a 变量,就会输出 函数中 a 变量的值,因为只有声明没有初始化,所以是 undefined.