The moment you declared a variable inside your function, it masked the global variable due to the way scoping rules work from inner to outer. So, function level var x variable is found first and not the global one that you expected.
JS converted your function from :
function(){
x = 7
var x;
console.log(x);
x = 10;
console.log(x);
}
to this:
function(){
var x;
x = 7
//var x;
console.log(x);
x = 10;
console.log(x);
}
To your question about updating x = 7 and then declaring var x, let me explain it a bit more.
Javascript code is not just executed, there is a compilation phase too. In this phase var declarations are looked inside the function(apart from so many other things that happen but I am just sticking to the question at hand).
If found, they are moved at the top of the function. This is called hoisting. At this time you can think that your code has been modified by JS(order of var declaration and assignment doesn't matter now). If you just think of code being interpreted then order matters but as I said, due to compilation phase the hoisting occurs and not thinking in these terms causes the confusion you have. Once you look at it from compilation, hoisting angle, things look clearer.
Hope it helps!
For further study, please read/listen Kyle Simpson who is authority on javascript.