js在html中函数调用为数组赋值失败?

想用for循环给数组赋值,发现如果在js中调用函数就能赋值成功,但在html中onload调用函数却失败了,这是什么原因?

var test = [];
function addArray(){
    for (var i = 0;i < 3;i ++){
    	test.push(i);
		}
	}
console.log(test);//输出为0.1,2

下面是html主体(已正确引用该js):

<body onload="addArray()">
</body>

而在html控制台中输出为空

检查下引用的js路径是否正确

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body onload="addArray()">

</body>
<script>
    var test = [];
    function addArray(){
        for (var i = 0;i < 3;i ++){
            test.push(i);
        }
        console.log(test);
    }
</script>
</html>

可以的

可是假如将console.log()移动到方法外就不行了,是不是说明没有实际上对数组进行修改?