The var directive is processed on the pre-execution stage, the b becomes local variable before document.write(b);, so at that time, b is undefined.
Note: assign a value to a variable is at the execution time, so you could image your code is like below.
function run(){
document.write(b);
var b=1;
}
is the same as:
function run(){
var b;
document.write(b);
b=1;
}
Addtion:
This is why Put every var definition at the top of the function is good practice.