题目一

var a = 1;
function fn1() {
    alert(a); 
    var a = 2;
}

fn1();
alert(a);

答案:

var a = 1;
function fn1() {
    alert(a);
    var a = 2;
}

fn1();  // undefined
alert(a);   // 1

题目二

var a = 1;
function fn1 (a){
    alert(a); 
    a = 2;
}

fn1(); 
alert(a);

答案:

var a = 1;
function fn1 (a){
    alert(a);
    a = 2;
}

fn1();  // undefined
alert(a);  // 1

题目三

var a;
if (true) {
	a = 5;
	function a(){}
	a = 0;
	console.log(a)
}
console.log(a)

答案:

var a;
if (true) {
	a = 5;
	function a(){}
	a = 0;
	console.log(a)   // 0
}
console.log(a)   // 5

参考链接:

  1. 【JS点滴】声明提前,变量声明提前,函数声明提前,声明提前的先后顺序.