global & local var and this operator in js
<p>I am a little bit confused with global, local var and this operator in js.
I have to code examples and their output, if someone can explain why the output is so:</p>

<pre><code>var x="10";
function f() {
    var x="4";
    alert(this.x);
    function g() {alert(x);}
    g();
}
f();
</code></pre>

<p>the output is: alert(10) and then alert(4), why? what is the difference with the this operator?</p>

<p>the secend example:</p>

<pre><code>enter code here

 var y="5";
var x="10";
x=y;
function f()
{
    y="4";
     alert(this.y);
     function g() {alert(y);}
     g();
}
alert(y);
f();
</code></pre>

<p>the output is:alert(4) then alert(5) anf then alert(4), why again? what the hidden semantic of "this" operator that I don't see?</p>

<p>if someone can give a general rules for global,local,this,scope in js and their extreme cases please.</p>