Can anyone explain the hoisting behavior in the below scenarios?

```
alert(x);
var x = 10;

Result: alert-undefined

```

In the below case, x is not defined using 'var' keyword - hence attaches to the global scope.

```
alert(x);
x = 10; // not given a var

Result: error

```

In case if we replace x with window.x, we get alert as undefined!

```
alert(window.x); // calling the variable with window namespace
x = 10; // not given a var

Result: alert-undefined

```

Can you explain why it is different in calling a variable (which is attached to global scope) with the variable name (x) and with window namespace (window.x)?